Functions

So far we have called some Objective-C functions but we have not defined any yet. Let's see what the Objective-C versions are of the functions we defined in Chapter 2, Building Blocks – Variables, Collections, and Flow Control.

Our most basic function definition didn't take any arguments and didn't return anything. The Objective-C version looks similar to the following code:

func sayHello() {
    print("Hello World!");
}
sayHello()
void sayHello() {
    NSLog(@"Hello World!");
}
sayHello();

Objective-C functions always starts with the type that the function returns instead of the keyword func. In this case, we aren't actually returning anything, so we use the keyword void to indicate that.

Functions that take arguments and return values have more of a disparity between the two languages:

func addInviteeToListIfSpotAvailable
    (
    invitees: [String],
    newInvitee: String
    )
    -> [String]
{
    if invitees.count >= 20 {
        return invitees
    }
    return invitees + [newInvitee]
}
addInviteeToListIfSpotAvailable(invitees, newInvitee: "Roana")
NSArray *addInviteeToListIfSpotAvailable
    (
    NSArray *invitees,
    NSString *newInvitee
    )
{
    if (invitees.count >= 20) {
        return invitees;
    }
    NSMutableArray *copy = [invitees mutableCopy];
    [copy addObject:newInvitee];
    return copy;
}
addInviteeToListIfSpotAvailable(invitees, @"Roana");

Again, the Objective-C version defines what it is returning at the beginning of the function. Also, just like variables, parameters to functions must have their type defined before their name instead of after. The rest however, is pretty similar: the arguments are contained within parentheses and separated by commas; the code of the function is contained within curly brackets and we use the return keyword to indicate what we want to return.

This specific implementation actually brings up an interesting requirement for dealing with arrays in Objective-C. Just like we want to avoid mutable arrays in Swift, we normally want to avoid them in Objective-C. In this case, we still don't want to modify the passed in array, we just want to add the new invitee to the end of a copied version. In Swift, because arrays are value types, the copy is made for us and we can use the addition operator to add on the new invitee. In Objective-C, we need to explicitly make a copy of the array. More than that, we need the copy to be mutable so that we can add the new invitee to it.

All in all, the biggest difference between Swift functions and Objective-C methods is the definition of the return value being at the beginning or the end of the parameters. The memory is handled in the same way in both languages. When passing in a pointer in Objective-C, the pointer itself is copied but both versions are going to reference the exact same instance. When a value type is passed into a function in Swift, the value is simply copied and the two versions have nothing to do with each other after that.

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

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