Use if statements. Please refer to the Discussion section of this recipe for more information about different scenarios under which you might want to use if statements.
We use if statements in our everyday conversations. For instance, you might say “If I get a hold of him, I’ll tell him...” or “I would put the computer to sleep if it didn’t take so long to come back up.” All these statements have a condition. If statements in Objective-C also have a condition. You can even make them more sophisticated, having your app do something if a condition is met and something else if the condition is not met. The basic form of an if statement is:
if
(
condition
){
/* Your code to get executed if the condition is met */
}
Note
As long as the condition is a value other than zero/nil/NULL, the code inside the if statement will run.
An if statement that has an “otherwise” clause in it is known as an if-else statement, and its format is:
if
(
condition
){
/* Your code to get executed if the condition is met */
}
else
{
/* Code to get executed if condition is not met */
}
The else
clause of the if-else
statement can also contain its own if statement! That might sound
strange, but consider this scenario. In real life, you can say something
similar to this: “I will go get a cup of coffee. If the place is open, I
will get a tall latte; if it’s closed and the other place is open, I
will get a cappuccino; otherwise, I will just come back home and make
tea for myself”. The part where we said “...if it’s closed and the other
place is open...” is an else statement with an if statement embedded in
it. Here is how you would implement that in Objective-C:
if
(
Coffee
place
A
is
open
){
Get
a
Tall
Latte
from
coffee
place
A
}
else
if
(
Coffee
place
B
is
open
){
Get
a
Cappuccino
from
coffee
place
B
}
else
{
Come
back
home
and
make
tea
}
The condition for an if statement, regardless of whether it is a
standalone if statement (like the first condition in the last example)
or embedded inside an else statement, must resolve to a boolean value. A
boolean value is either YES
or
NO
. For instance, the following code
will always get executed, regardless of which
condition/device you run it on:
if
(
YES
){
/* This code will get executed whenever the app gets to it */
}
else
{
/* The app will NEVER get here */
}
The reason behind this is that the condition for the if statement
in this example is always met as long as the YES
is the condition. To make things more
exciting, you can do comparisons in the condition supplied to an if
statement, like so:
NSInteger
integer1
=
123
;
NSInteger
integer2
=
456
;
if
(
integer1
==
integer2
){
NSLog
(
@"Integers are equal."
);
}
else
{
NSLog
(
@"Integers are not equal."
);
}
Warning
Note that a double equal sign is used inside the conditional. A common error is to use a single equal sign, which is a totally different operator. The double equal sign does a comparison and returns a Boolean result, which is what you normally want in a conditional. A single equal sign changes the value of the left-hand variable and returns the value set, which the condition tries to interpret as a Boolean value. Although occasionally appropriate, it’s usually a serious error to use a single equal sign.
If you are comparing objects, it is best to use the isEqual:
instance method of the NSObject
class:
NSObject
*
object1
=
[[
NSObject
alloc
]
init
];
NSObject
*
object2
=
object1
;
if
([
object1
isEqual:
object2
]){
NSLog
(
@"Both objects are equal."
);
}
else
{
NSLog
(
@"Objects are not equal."
);
}
Note
For now, you don’t have to worry about what objects are. This will be explained in detail in other recipes in this chapter.
Some objects such as strings, however, have their own comparison methods, changing the
way we compare two strings. For instance, you can have two string
objects that contain the same characters. If you compare them using
their isEqual:
instance method, you
will get the result NO
, because they are different
objects. However, they might still contain the exact same characters.
Because of this, different classes expose their own comparison methods
in Objective-C. For more information about classes, please refer to
Recipe 1.12. To learn more about objects,
refer to Recipe 1.15.
An if statement and its else statement can be written with or without curly braces. Using the former syntax (with curly braces), you can execute multiple lines of code after the condition is satisfied. However, without curly braces, you can write only one line of code for each condition. Here is an example of the latter syntax without curly braces:
NSString
*
shortString
=
@"Hello!"
;
if
([
shortString
length
]
==
0
)
NSLog
(
@"This is an empty string"
);
else
NSLog
(
@"This is not an empty string."
);
Warning
Be extra careful with logging and if statements without curly
braces. Often, when a product goes to production, a production manager
might attempt to comment out all your NSLog
methods simply by replacing all
occurrences of NSLog
with //NSLog
. If you have if statements without
curly braces, as in our last example, the production manager’s
commented-out code will look like this:
NSString
*
shortString
=
@"Hello!"
;
if
([
shortString
length
]
==
0
)
//NSLog(@"This is an empty string");
else
//NSLog(@"This is not an empty string.");
This will break the code and people in the company will not be happy. It doesn’t matter whether they are not happy at you or not happy at the production manager. That would be a team effort gone wrong, so you will all be to blame. To avoid this, make sure that you always write your if statements with curly braces.
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.