What is a Variable?A variable is an identifier or label that we, as programmers, can give to an area of computer memory. Each variable has a name (such as accountBalance ) and a type that indicates the kind of data we are storing in the variable (such as a number) and when we run the app, the variable is assigned a value (such as 12345.67 ). When the app runs we can use the name of the variable to access the value that is currently stored in it. This includes both reading the value in the variable and changing the value that is stored.
Declaring a VariableBoth the Objective-C and the C language on which it is based, are statically-typed languages. This means that each variable you want to use in your application, must be declared before it is used and as part of that declaration, you must inform the compiler about what type of information that variable is going to hold. Once the variable has been declared, we can obviously change the value that is stored in the variable (hence, the name variable), but we can’t change the type of data that is stored in it. The type of data that is stored in the variable is essentially fixed or ‘static’. Lets take a look at a couple of examples of variable declarations. Variable declarations in your code will have the following syntax: The assignment of an initial value to the variable (by including the assignment operator (=) followed by a value) is optional. If you choose to exclude this (by not including the assignment operator or the initial value), you must still finish the statement with a semi-colon (;). For a more concrete example, the following declares a variable called numberOfMoves and indicates it is of type int . The int type is one of many you have available to you in Objective-C. We’ll take a closer look at the range of different types in another post. In this example, we choose not to initialize the variable with a value:
int numberOfMoves;In the following example however, we declare a variable livesRemaining , again of type int but this time we initialise it with a value of 3 using the assignment operator (=).
int livesRemaining = 3;In similar fashion, we can also create variables that hold floating point values (i.e. values containing a decimal point) and just to illustrate, we also change the value of the variable after it has been declared, again using the assignment operator:
// Declares the variable and initializes it to 100.0. float healthPercentage = 100.0;
// Updates the value of the healthPercentage variable to 76.5. healthPercentage = 76.5;If we’re declaring multiple variables of the same type, we also have the option of combining them into a single declaration statement. The following example declares two variables x and y , both of type int , and initializes x to 123 and y to 321 .
int x = 123, y = 321;
Why Do We Declare Variables?There are a couple of reasons why we have to declare our variables to the compiler. Reason 1: When you declare a variable, the compiler needs to allocate an area of memory for that variable, memory in which we can store the variables value. But how much memory should the compiler allocate? Variable declarations solve this problem as the data type we specify in the declaration is used by the compiler to determine the amount of memory that it needs to allocate. Reason 2: Declaring variables in our Objective-C code helps to eliminate mistakes. When we tell the compiler about the variables we are going to use and the types of data we are going to store in them, the compiler is able to check the source code we have written and inform us of any errors or potential problems. An example of this might be if we had declared a variable as holding text such as Welcome but then tried to do a division operation on that text. If this occurred, the compiler would be able to analyse our code, and warn us of a potential problem. This is a major benefit and helps us avoid mistakes in our code.
Rules for Naming VariablesWhen you come to choosing variable names in your Objective-C code, there are a number of rules that you need to comply with. These are:
- Variables names in Objective-C (as well as in C), must consist of letters, numbers and the underscore character (_).
- The first character of the variable name must be a letter or an underscore.
- Variable names are case sensitive for example, highScore and HighScore are viewed as different variables by the compiler.
- Variables cannot contain white space in the middle of them (i.e. a variable name of time remaining would be illegal due to the space in the name).
a hitRate kmPerHour high_scoreThe following names are however illegal:
1stUser // Starts with a number. salt&pepper // Contains a character that is not a letter, number or underscore. bank account // Contains an illegal space.When we come to declare variables in our code, there are a number of conventions that are used by C and Objective-C programmers. In C code, programmers commonly use the convention of splitting long variable names with an underscore character. For example:
lives_remaining network_connectionIn Objective-C however, programmers usually use an approach called CamelCase for variable names. In CamelCase, the first character in the variable name is lowercase and subsequent words in the variable name are indicated by the use of a capital letter e.g.
playerAvatar gameManagerAnother convention is that variables whose names begin with an underscore, are traditionally seen as private variables, variables used only in the implementation of your application and therefore variables that should not be used in a wider context. This use of the leading underscore is however only convention and is not actually enforced by the compiler. Source Image: https://flic.kr/p/dGFJ4M