Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Parameters and Tuples Tuples

Collin Keating
Collin Keating
1,797 Points

I don't understand how to place a return value, into a variable? In iOS

How to I add the return value to a variable

tuples.swift
func greeting(person: String) -> (greeting: String, language: String) {

    return (greeting,language)
    var result = (greeting,language)
}

greeting("Tom")

2 Answers

Hi Collin

So, to declare a variable you use the keyword var followed by blank space and the name of the variable, right?

var someVariable

To give this variable a value you simply add an equals sign and whatever value you like, yes?

var someVariable = "Some Value"

In your case you want to think of greeting("Tom") as if it is already a value that you can assign to the variable.

var result = greeting("Tom")

Now result holds the value returned from the greeting function - understand?

Best of luck with swift/iOS

func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language)
    }
    var result = greeting("Tom")
    println(result.language)