1.7. Declaring Variables in Objective-C

Problem

You want to use variables with clear names in your iOS apps.

Solution

Apple conventions dictate certain rules for variable names. Determine the type of the variable (for instance, integer, array, string, etc.) and then a descriptive name for it. In an empty line of code, place the type of your variable first, following by its name. Variable names are advised to follow these rules:

  1. Follow the camelCase naming convention. If the variable name is one word, all letters must be lowercase. If the variable name is more than one word, the first word must be entirely lowercase and the subsequent words must have their first letter uppercase and the rest of their letters lowercase. For instance, if you want to have a counter variable, you can simply name it counter. If you want to call your variable “first counter”, declare it firstCounter. The variable name “my very long variable name” would become myVeryLongVariableName. (Names of that length are quite common in iOS programs.)

  2. Variables should ideally have no underline in their names. For instance, my_variable_name can and perhaps should be changed to myVariableName.

  3. Variable names should contain only letters and numbers (no punctuation such as commas or dashes). This is a restriction in the Objective-C language and many other languages, but this helps keeping variable names tidy.

Let’s have a look at a few examples. There are a few primitive data types in Objective-C. A data type is a name that specifies the type of a variable. For instance, you can say that you have an integer variable of type NSInteger or an integer variable of type NSUInteger, where the former is a signed variable and the latter is unsigned (note the “U” after “NS”) in the latter example).

Note

Signed integers can contain negative numbers, whereas unsigned integers cannot.

We can then define these variables in our source code like so:

NSInteger signedInteger = -123;   /* Can take negative numbers */
NSUInteger unsignedInteger = 123; /* Cannot take negative numbers */

There are certain rules, as mentioned before, about naming your variables (for instance, the camelCase rule). However, other aspects of your variable naming convention depend entirely on your choice. In general, I advise that you always assume you are working for a big organization (whether it is your own company or somebody else’s company) and follow these rules:

  1. Give descriptive names to your variables. Avoid names such as “i” or “x.” These names will never make any sense to anybody else but yourself and chances are that they won’t make sense to you either if you leave the project for some time and come back to it after a few months. The compiler doesn’t really care if the variable name is 50 letters long. If that makes your variable name more descriptive and you cannot do without it, then go for it.

  2. Avoid creating vague variable names. For instance, if you have a string variable and you want to place the full name of a person in that variable, avoid giving it names such as “theString” or “theGuy” or “theGirl.” These make no sense at all. It is best to give a name such as “fullName” or “firstAndLastName” rather than something that confuses everybody who looks at your source code, including yourself!

  3. Avoid giving names to your variables that are likely to lead to mistyping. For instance, it is much better to call your variable “fullName” rather than “__full______name.” It is best to avoid underlines in variables all together.

Discussion

Variables are placeholders for data that you want to hold in memory. For instance, if you want to delete 100 files from the disk and those files are named 1.png, 2.png, on to 100.png, it would be best to create a counter variable that starts from 1 and goes all the way to 100 and then use the content of that variable (the number) to delete the files. Programmers use variables to do arithmetic or simple operations, such as prefixing the last name of a person with their first name to create the resulting full name.

Every variable has to have a type. The type of a variable tells the compiler and the machine that runs that program, what type of variable that is and what sort of value it holds. For example, a variable that is an integer can hold the value 123 but cannot hold a value with decimal places such as 123.456. For the latter, we will need a floating-point variable. Integer and floating-point here are the data types and, in Objective-C, are defined with NSInteger and float. Here are some of the commonly used data types in Objective-C:

NSInteger

Variables of this type can store signed (positive or negative) integer values.

NSUInteger

Variables of this type can store unsigned (only positive or zero) integer values.

float

Variables of this type can store floating-point values (values that have decimals in them, such as 1.23 or 73.12).

NSString

Variables of this type can store strings, such as “my string” or “Mrs Thomson.”

NSArray

Variables of this type can store an array of objects. For instance, if you have read 10 files from the disk, you can store their data in an array. An array can contain more than one instance of the same value.

NSSet

Variables of this type can store unique instances of variables. Sets are similar to arrays in that they can store multiple value but each value can only appear at most once in a set.

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.