Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Top 5 Must Know Variable Types For iPad (& iPhone) Programming

2.67/5 (3 votes)
26 May 2010CPOL3 min read 13K  
Top 5 Must Know Variable Types For iPad (& iPhone) Programming

Post image for Top 5 Must Know Variable Types For iPad (& iPhone) Programming

Last week, I was working on my upcoming book with my new publisher and I was writing a chapter on variable types. Variables in Objective-C confused me at first because there are a few options (that follow different rules) that you have available to you. So, I thought it could be helpful to new programmers to get to know the 5 most common variable types used in iPhone and iPad development.

int

Integers are whole numbers (numbers without decimal places) declared with the keyword int. These types are used frequently in Cocoa-Touch classes like NSArray to report the number of elements in an array. Using int (as well as other number types below) is often simpler than trying to use NSNumber in many situations when you must work with numbers. Here is how you use an integer:

C++
int i;
i = 5;
NSLog(@"i = %i", i);

float (and double)

When you need to use numbers with decimal places, you must use the float (or double below) primitive type. Float types are used to represent numbers like 12.3456. If you plan on doing many numerical calculations, you may need to use this type. Here is how you use float:

C++
float f;
f = 12.78;
NSLog(@"f = %f", f);

Like float above, double types are used to represent numbers with decimal places. For the most part, you will use float and double interchangeably in typical iPhone apps but you will probably see both used:

C++
double d;
d = 1.99;
NSLog(@"d = %f", d);

BOOL

BOOL represents a yes or no, true or false or off and on condition. In Objective-C, we use YES or NO (like an answer to a question) to represent on or off, true or false or 1 or 0 respectively. In the background, BOOL acts like an int type so you must use the %i to test for a BOOL type’s value in NSLog. Here is how it works:

C++
BOOL answer;
answer = YES;
NSLog(@"The answer is %i", answer);

NSObject

The types discussed above are all what are called primitive types which are basic data types that are simple to use. But, iPhone OS requires object oriented features so we must also be able to declare and use object variable as well. Now these will be a bit more complicated since we need to allocate memory and use a sort of pointer variable. Luckily for us, the pattern is usually the same as what we are about to do for NSObject since most classes in Objective-C are derived from NSObject:

C++
NSObject *object;
object = [[NSObject alloc] init];
NSLog(@"object = %@", object);
[object release];

id

Sometimes you know that you will be working with an object but you are not sure what that object will be when the code executes. These situations call for the id variable which you can use as a sort of placeholder for any object. id is used often in the Cocoa-Touch frameworks and the use of id is one of the things that contributes to the powerful flexibility of the Objective-C programming language. Here is how you would use id along with the NSObject type created above:

C++
id someObject;
someObject = object;
NSLog(@"some object = %@", someObject);

NOTE: Be careful if you are following along in the code to wait to send the release message to object before using the id example.

What Other Types Do You Find In iPhone OS?

Let us know other major types that you run into over and over again in the comments below!

Please share this if you like it! Digg del.icio.us Facebook Mixx Google Bookmarks email FriendFeed LinkedIn MySpace Ping.fm StumbleUpon Suggest to Techmeme via Twitter Technorati Tumblr Yahoo! Bookmarks

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)