Working with Variables and Constants in Objective-C
Previous | Table of Contents | Next |
Objective-C 2.0 Data Types | Objective-C Operators and Expressions |
Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters. |
In the previous chapter we looked at the basic data types supported by Objective-C. Perhaps the second most basic aspect of programming involves the use of variables and constants. Even the most advanced and impressive programs, from high end video games to enterprise commerce applications, use variables in one form or another. In this chapter of Objective-C 2.0 Essentials we will cover everything that an Objective-C programmer needs to know about variables.
What is an Objective-C Variable
<google>IOSBOX</google> Variables are essentially locations in computer memory that are reserved for storing the data used by an application. Each variable is given a name by the programmer and assigned a value. The name assigned to the variable may then be used in the Objective-C code to access the value assigned to the variable. This access can involve either reading the value of the variable, or changing the value. It is, of course, the ability to change the value of variables which gives them the name variable.
A variable must be declared as a particular type such as an integer, a character, a float or double. Objective-C is what is known as a strongly typed language in that once a variable has been declared as a particular type it cannot subsequently be changed to a different type. While this may come as a shock to those familiar with loosely typed languages such as Ruby it will be familiar to Java, C, C++ or C# programmers. Whilst it is not possible to change the type of a variable it is possible to disguise the variable as another type under certain circumstances. This involves a concept known as type casting and will be covered later in this chapter.
Variable declarations require a type, a name and, optionally a value assignment. The following example declares an integer variable called interestRate but does not initialize it:
int interestRate;
The following example declares and initializes a variable using the assignment operator (=):
int interestRate = 10;
Similarly, a new value may be assigned to a variable at any point after it has been declared.
double interestRate = 5.5456; //Declare the variable and initialize it to 5.5456 interestRate = 10.98; // variable now equals 10.98 interestRate = 20.87; // variable now equals 20.87
What is an Objective-C Constant?
<google>ADSDAQBOX_FLOW</google> A constant is similar to a variable in that it provides a named location in memory to store a data value. Constants differ in one significant way in that once a value has been assigned to a constant it cannot subsequently be changed.
Constants are particularly useful if there is a value which is used repeatedly throughout the application code. Rather than use the value each time, it makes the code easier to read if the value is first assigned to a constant which is then referenced in the code. For example, it might not be clear to someone reading your Objective-C code why you used the value 5 in an expression. If, instead of the value 5, you use a constant named interestRate the purpose of the value becomes much clearer. Constants also have the advantage that if the programmer needs to change a widely used value it only needs to be changed once in the constant declaration and not each time it is referenced.
As with variables, constants have a type, a name and a value. Unlike variables, constants must be initialized at the same time that they are declared and must be prefixed with the const keyword:
const int interestRate = 10;
Once declared, it is not possible to assign a new value to the constant. The following code will cause the Objective-C compiler to report an error that reads "error: assignment of read-only variable":
const int interestRate = 10; interestRate = 5; // invalid attempt to assign new value to read-only const
Note that the value of a constant, unlike a variable, must be assigned at the point it is declared. For example, the following code will not compile:
const int interestRate; interestRate = 10; // invalid attempt to initialize constant after declaration
The above code will, once again, result in a compilation error.
Type Casting Objective-C Variables
As previously mentioned, Objective-C is a strongly typed language. In other words, once a variable has been declared as a specific data type, that type cannot be changed. It is possible, however, to make a variable behave as a different type using a concept known as type casting. Suppose we have two variables declared as doubles. We need to multiply these together and display the result:
double balance = 100.54; double interestRate = 5.78; double result = 0; result = balance * interestRate; NSLog(@"The result is %f", result);
When executed, we will get the following output:
The result is 581.121200
Now, suppose that we wanted the result to the nearest whole number. We can achieve this by casting the type of both double values to type int within the arithmetic expression:
double balance = 100.54; double interestRate = 5.78; double result; result = (int) balance * (int) interestRate; NSLog(@"The result is %f", result);
When compiled and run, the output will now read:
The result is 500.000000
It is important to note that type casting only changes the way the value is read from the variable on that one occasion. It does not change the variable type or the value stored in any way. After the type cast, balance is still a double and still contains the value 100.54.
<google>BUY_OBJC_BOTTOM</google>
Previous | Table of Contents | Next |
Objective-C 2.0 Data Types | Objective-C Operators and Expressions |