FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How can you return two or more values from a function?

Paul Hudson    @twostraws   

Swift’s functions have a single return type, such as Int or String, but that doesn’t mean we can only return a single value. In fact, there are two ways we can send back multiple pieces of data:

  • Using a tuple, such as (name: String, age: Int)
  • Using some sort of collection, such as an array or a dictionary.

If you had a hard time understanding why tuples were important, this is a really good example of a place where they really stand out.

If we wanted to make a function that returns a person’s first name and last name, we could do it like this:

func getUser() -> [String] {
    ["Taylor", "Swift"]
}

let user = getUser()
print(user[0])

That sends back an array containing two strings and stores it in user, which does the job. However, the code is problematic for two reasons:

  1. Some cultures write their last names first (“Swift Taylor”), so it’s possible other developers might expect index 0 to be the last name and index 1 to be the first name.
  2. What if we inserted a middle name? It would push “Swift” back from index 1 to index 2, causing problems.

A second pass at the function might look like this:

func getUser() -> [String: String] {
    ["first": "Taylor", "last": "Swift"]
}

let user = getUser()
print(user["first"])

That’s definitely better, because now we can be sure exactly where to find each value: user["first"] will always be “Taylor” no matter whether we change the dictionary order or if we insert a middle name.

However, this solution still isn’t ideal:

  1. We’re need to look for values using strings, which means user["First"] will fail because we wrote “First” rather than “first”.
  2. Our dictionary might contain neither value – maybe our user was Prince, Plato, Beyonce, or someone else who goes by only one name.
  3. Every time we read a value from the dictionary we need to unwrap the optional, because Swift can’t be sure the value is definitely there.

Tuples can solve the problem by letting us be specific about what data will come back: its name, its type, its order, and whether it’s optional or not. This means we can write functions to return multiple values much more safely:

func getUser() -> (first: String, last: String) {
    (first: "Taylor", last: "Swift")
}

let user = getUser()
print(user.first)

Now we’ve written code that is immune to all our problems:

  1. Our data must be returned first name first and last name second.
  2. If we insert a middle name it will not affect the position of other values.
  3. We can’t make case-sensitive mistakes while reading values.
  4. There's no way we won’t get back a first name or last name – if someone only uses one name then we’ll get back an empty string.
  5. There is no optionality.

So, tuples make fantastic candidates for returning multiple values from functions.

Hacking with Swift is sponsored by Stream

SPONSORED Stream’s latest iOS Chat SDK release provides a better developer experience with new docs, customizable attachments, and UI components, and under-the-hood performance improvements.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.