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

what is the secret with tuples

stumped with every step. I have watched the video twice and spent 20 minutes on this.

tuples.swift
func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"
    let person = "Tom"
    var result = (greeting, language)

    return result

4 Answers

Daniel Sattler
Daniel Sattler
4,867 Points

Important is the right order of everything:

func greeting(#person: String) -> (language:String,greeting:String) {
    let language = "English"
    let greeting = "Hello \(person)"
    var greet = (greeting,language)
    return greet
}
var result = greeting(person: "Tom")
println(result.1)

Try this

I notice you added the hashtag for person. What does that do?

Daniel Sattler
Daniel Sattler
4,867 Points

it turns the value person into a named value. If i remember correct it was required in the challenge.

func greeting(#person: String)

If you do so, by calling the function, you will have to pass the named value

greeting(person: "Tom")

if you do it this way

func greeting(person: String)

you can do it this way

greeting("Tom")

Thanks Daniel.

I moved on to Optionals and got stuck on the second challenge now. The system keeps spitting me out when I try to correct and making me start over. Getting frustrated.

Can you point me in the right direction with this code. The editor keeps asking if I've used println().

func search(#name: String) -> String? { let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"] for n in names { if n == name { return n } } return nil

// let dwarf = search (name:"") //dwarf!

if let result = search(name:"Doc") { println("Found (result)")

} }

Daniel Sattler
Daniel Sattler
4,867 Points

Ok... a few issues here:

first of all, code after "return" will never be executed

second Your string interpolation is incorrect.

You say

println("Found (result)")

You forgot a back slash here

println("Found \(result)")

Try it this way

func search(#name: String) -> String? {
    let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
    for n in names {
        if n == name {
            return n
        } else {
            return nil
        }
    }
    return name


}
if let result = search(name:"Doc") {
    println("Found \(result)")

}

you need to ad your if let AFTER the function ;-)

I got it Daniel. the println command only wanted "Found" without the value. I hate being so needy but there's no way to move on without getting thru the challenges. Thanks for hanging in there with me.

{ println("Found") }