Functions

This page shows the old Objective-C way of creating and calling functions and also similar ways to achieve the same thing in Swift.

This guide will be updated in the future as we dive deeper into Swift, be sure to bookmark this page and check back from time to time.

If you notice something that is missing from this page or spot an error, please let us know!

Simple Function

Objective-C

-(void)printText {

    NSLog(@"Printing this line to the console");

}

Swift

func printText(){

    print("Printing this line to the console")

}

Simple function with parameter

In this example, we want to pass a name (String) to a function and then print it out to the console.

Objective-C

-(void)outputName:(NSString *)nameString {

   NSLog(@"My name is %@",nameString);

}

To call this method within the same class:

[self outputName:@"Bob"];
//Results in the outputName function being called and "My name is Bob" printed to the console.

Swift

In Swift, we define the input value first (in our case, the string value), followed by the Type (String).

func outputName(nameString:String){

    print("My name is: \(nameString)")

}

To call this method within the same class:

outputName(@"Bob")
//Results in the outputName function being called and "My name is Bob" printed to the console.

Simple function with return value

In this example, we want to our function to get the current time, formatted and returned as a String.

Objective-C

-(NSString *)getCurrentTime {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];

    return currentTime;
    
}

To call this method within the same class:

NSString *time = [self getCurrentTime];
NSLog(@"%@",time);
//Results in the getCurrentTime function being called and the current time printed to the console.

Swift

In Swift, we include the return arrow “->” which specifies that the function will return and the type of object that will be returned (String).

    func getCurrentTime() -> String {
        
        let date = NSDate()
        let formatter = NSDateFormatter()
        formatter.timeStyle = .ShortStyle
        var stringValue = formatter.stringFromDate(date)
        
        return stringValue
        
    }

To call this method within the same class:

let time = getCurrentTime()
print(time)
//Results in the getCurrentTime function being called and the current time printed to the console.

Function with multiple return values

In this example, we want to our function to get the current time and the date formatted as Strings.

Objective-C

Objective-C doesn’t support a function returning multiple values. The closest you can get would be to create your values add them to a NSDictionary and return the dictionary.

Swift

In Swift, we include the return arrow “->” which specifies that the function will return and a tuple with the types we wish to be returned. A tuple type is simply a comma-separated list of zero or more types, enclosed in parentheses.

    func getCurrentDateAndTime() -> (date: String, time: String) {
        
        let date = NSDate() //Get current date
        
        //Formatter for time
        let formatterTime = NSDateFormatter()
        formatterTime.timeStyle = .ShortStyle //Set style of time
        var timeString = formatterTime.stringFromDate(date) //Convert to String


        //Formatter for date
        let formatterDate = NSDateFormatter()
        formatterDate.dateStyle = .ShortStyle //Set style of date
        var dateString = formatterDate.stringFromDate(date) //Convert to String

        return (dateString, timeString) //Returns a Tuple type
        
    }

To call this method within the same class:

        let time = getCurrentDateAndTime()
        print(time.date)
        print(time.time)
//Results in the getCurrentDateAndTime function being called and the current date & time printed to the console. Notice how simply the values can be accessed from the returned Tuple;

Leave a Comment