Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 26 May 2010
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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:

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:

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:

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)

Share

About the Author

MobileAppMastery

United States United States
No Biography provided

Comments and Discussions

 
GeneralPerhaps probe a little deeper... PinmemberDaRabman8-Jun-10 2:05 
GeneralMy vote of 1 PinmemberKarstenK31-May-10 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 26 May 2010
Article Copyright 2010 by MobileAppMastery
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid