Clicky

iOS Dev Nugget 137 Named Tuple Elements

.

Need to run a code review on your codebase? Hire me

Tuples are a handy Swift construct that is especially useful for functions that return multiple values. You can use it like so:

func f1() -> (Int, Int, Int) {
    return (111, 222, 333)
}

var (a, b, c) = f1() //a, b, and c now stores the value 111, 222, and 333 respectively.

func f2() -> (Int, Int, Int) {
    return (111, 222, 333)
}

var x = f2() //Access x.0, x.1, x.2

Or you can give the elements in the tuple a name:

func f3() -> (aa: Int, bb: Int, cc: Int) {
    return (111, 222, 333)
}

var y = f3() //You can use y.aa, y.bb, y.cc (and y.0, etc still work)


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues