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...