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

Matthew Campbell, May 25th, 2010

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.

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:

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:

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:

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:

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:

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:

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

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

[...] here: Top 5 Must Know Variable Types For iPad (& iPhone) Programming Categories: Me, Reading Tags: code, google-bookmarks, iphone programming, number, plan-on-doing, [...]

Hey fellow developers. Just wanted to drop a little note to help you out. I have started using Elance to fill some gaps in time I have and make some money doing it. This site is awesome, I have tons of iPhone projects now and making a lot of money doing it. I have use other sites but Elance has a great setup and a great great business model. If you are looking for extra work, then this is the place to do it. Thanks.

Thanks Matt. Great site!

What about currency? Do you recommend using Double vs. NSDecimal?

How about a few examples (or even a tutorial) on using NSDecimal?

Donovan

@donovan – for currency formatting I simply use NSNumber and NSNumberFormatter together. If you it this way your currency will always be localized for whatever country the device is in.

Example:

You must be logged in to post a comment.

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.