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:
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.)
Variables should ideally have no underline in their names. For instance,
variable_namecan and perhaps should be changed to
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
where the former is a signed variable and the latter is unsigned (note
the “U” after “NS”) in the latter example).
We can then define these variables in our source code like so:
/* Can take negative numbers */
/* 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:
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.
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!
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.
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
float. Here are some of the commonly used data
types in Objective-C:
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.