Control flow

Objective-C has many of the same control flow paradigms as Swift. We will go through each of them quickly, but before we do, let's take a look at the Objective-C equivalent of print:

var name = "Sarah"
println("Hello (name)")
NSString *name = @"Sarah";
NSLog(@"Hello %@", name);

Instead of print, we are using a function called NSLog. Objective-C does not have string interpolation, so NSLog is a somewhat more complex solution than print. The first argument to NSLog is a string that describes the format to be printed out. This includes a placeholder for each piece of information we want to log that indicates the type it should expect. Every placeholder starts with a percent symbol. In this case, we are using an at-symbol to indicate what we are going to be substituting in a string. Every argument after the initial format will be substituted for the placeholders in the same order they are passed in. Here, this means that it will end up logging Hello Sarah just like the Swift code.

Now, we are ready to look at the different methods of control flow in Objective-C.

Conditionals

A conditional looks exactly the same in both Swift and Objective-C except parentheses are required in Objective-C:

var invitees = ["Sarah", "Jamison", "Roana"]
if invitees.count > 20 {
    print("Too many people invited")
}
NSArray *invitees = @[@"Sarah", @"Jamison", @"Roana"];
if (invitees.count > 20) {
    NSLog(@"Too many people invited");
}

You can also include those parentheses in Swift, but they are optional. Here, you also see that Objective-C still has the idea of the dot syntax for calling some methods. In this case, we have used invitees.count instead of [invitees count]. This is only an option when we are accessing a property of the instance or we are calling a method that takes no arguments and returns something, as if it were a calculated property.

Switches

Switches in Objective-C are profoundly less powerful than switches in Swift. In fact, switches are a feature of strict C and are not enhanced at all by Objective-C. Switches cannot be used like a series of conditionals; they can only be used to do equality comparisons:

switch invitees.count {
    case 1:
        print("One person invited")
    case 2:
        print("Two people invited")
    default:
        print("More than two people invited")
}
switch (invitees.count) {
    case 1:
        NSLog(@"One person invited");
        break;

    case 2:
        NSLog(@"Two people invited");
        break;
        
    default:
        NSLog(@"More than two people invited");
        break;
}

Again, parentheses are required in Objective-C, where they are optional in Swift. The most important difference with Objective-C switches is that by default, one case will flow into the next unless you specifically use the break keyword to get out of the switch. That is the opposite of Swift, where it will only flow into the next case if you use the fallthrough keyword. In practice, this means that the vast majority of Objective-C switch cases will need to end with break.

Objective-C switches are not powerful enough to allow us to create cases for ranges of values and certainly cannot test a list of arbitrary conditionals like we can in Swift.

Loops

Just like conditionals, loops in Objective-C are very similar to Swift. While-loops are identical except that the parentheses are required:

var index = 0
while index < invitees.count {
    print("(invitees[index]) is invited");
    index++
}
int index = 0;
while (index < invitees.count) {
    NSLog(@"%@ is invited", invitees[index]);
    index++;
}

The for-in loops are slightly different, in this you must specify the type of the variable you are looping through with the following:

var showsByGenre = [
    "Comedy": "Modern Family",
    "Drama": "Breaking Bad"
]
for (genre, show) in showsByGenre {
    print("(show) is a great (genre)")
}
NSDictionary *showsByGenre=@{
    @"Comedy":@"Modern Family",
    @"Drama":@"Breaking Bad"
};
for (NSString *genre in showsByGenre) {
    NSLog(@"%@ is a great %@", showsByGenre[genre], genre);
}

You may have also noticed that when we are looping through an NSDictionary in Objective-C you only get the key. This is because tuples do not exist in Objective-C. Instead, you must access the value from the original dictionary, using the key as you loop through.

The other feature that is missing from Objective-C is ranges. To loop through a range of numbers, Objective-C programmers must use a different kind of loop called a for loop:

for number in 1 ... 10 {
    print(number)
}
for (int number = 1; number <= 10; number++) {
    NSLog(@"%i", number);
}

This loop is made up of three parts: an initial value, a condition to run until, and an operation to perform after each loop. This version loops through the numbers 1 to 10 just like the Swift version. Clearly, it is still possible to translate the Swift code into Objective-C; it just isn't as clean.

Even with that limitation, you can see that Objective-C and Swift loops are pretty much the same except for the parentheses requirement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset