C
iOS Developer Tips - Visitor Stats:
|
|
|
Converting Between C and Objective-C Strings (NSString)
Given the foundation of Objective-C is built upon the C programming language, it’s good to know how to convert C based strings to NSString objects, and vice-versa. For clarification: A C string is simply a series of characters (a one-dimensional character array) that is null-terminated, whereas an NSString object is a complete object with class [...]
Using #error and #warning Compiler Directives
Although not your everyday directives, #error and #warning definitely have there place. Let’s take a short look at a few examples where you might find these directives helpful. #error When the preprocessor runs into the #error directive, it will write an error to the console and generate a compile time error. You can also include [...]
Comment Out Large Blocks of Code with Nested Comments
In working with a large chunk of code recently, I needed to comment out a sizeable block to verify something was working as expected. My first thought was to simply to embed the offending code within /* and */
C Conditional Operator aka Ternary Operator
I’ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator. The conditional looks as follow: condition-test ? first-expression : second-expression If the condition-test is nonzero, the first-expression [...]
Getting a Random Number
If you need to generate a random number within your iPhone application, you have to push aside Objective-C, as there is not a class with a method for generating random numbers (as in Java). The alternative is to use C, among the functions available are: rand(), srand(), random(), srandom() and arc4random(). arc4random() tends to be [...]
Toggle an Integer Between 1 and 0
If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (^) operator in C to get the job done. In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo [...]
Working with Bitfields
Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact. The [...]
CGRect, CGSize and CGPoint Functions
With an understanding of C structures, and the definitions of CGRect, CGSize and CGPoint behind us, let’s look at a handful of functions for working with these structures.
CGRect, CGSize and CGPoint
Digging into development of iPhone applications, you’ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (see this post for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined:
C Structures
Leading up to a post on working with CGRect, CGPoint and CGSize, it makes sense to visit C structures. A structure is a collection of variables, grouped together to facilitate organization of data. For example, one might define a set of x and y coordinates as follows:






