O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning multiple values from a function

There are a couple of ways to return multiple values from a Swift function. One of the most common ways is to put the values into a collection type (an array or dictionary) and then return the collection. The following example shows how to return a collection type from a Swift function:

func getNames() -> [String] { 
  var retArray = ["Jon", "Kim", "Kailey", "Kara"]  
  return retArray 
} 
 
var names = getNames() 

In the preceding example, we declared the getNames() function with no parameters and a return type of [String]. The return type of [String] specifies the return type to be an array of string types.

In the preceding example, our array could only return string types. If we needed to return numbers with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required