Nice to know about variable type For iPhone & iPad

This article will surely helps new programmer to work with variables in Objective-C These are the most common variable types used in iPad and iPhone App 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 = 6;
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 = 15.68;
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 = 4.99;
NSLog(@"d = %f", d);

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 oneObject;
oneObject = object;
NSLog(@"One object = %@", oneObject);

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];

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 question;
question = YES;
NSLog(@"The question is %i", question);
-HS
Author: SPEC INDIA


less words, more information

Tech
IN 200
words

Read our microblogs

Subscribe Now For Fresh Content

Guest Contribution

We are looking for industry experts to contribute to our blog section through fresh and innovative content.

Write For Us

Our Portfolio

Proven Solutions Across Industries
Technology for Real-Life

Visit Our Portfolio

Scroll Up