Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuples with Swift

Tags:

swift

tuples

Hey so i've spent more than two hours trying to figure this out and I just can't get it right. I'm guessing i'm making a really simple mistake so if anyone can just point me in the right direction i'd really appreciate it, thanks! Btw this is a Treehouse course.

"Currently our greeting function only returns a single value. Modify it to return both the greeting and the language as a tuple. Make sure to name each item in the tuple: greeting and language. We will print them out in the next task."

func greeting (language: String, greeting: String) -> (String, String) {

    let language = "English"
    let greeting = "Hello"

    var found = ("\(language)", "\(greeting)")

    return found
}

The error message i'm getting is

swift_lint.swift:13:12: error: '(String, String)' is not convertible to 'String'
    return found
           ^

Now in the course work they converted a String and Bool so that worked but they didn't explain what to do when you have two of the same type. I assumed it was to convert it to (String, String) but I get that error.

Thanks for any help!

like image 923
tone10lite Avatar asked Jul 16 '26 02:07

tone10lite


1 Answers

I think you put the labels that were meant for the tuple in the wrong place - where the parameters go. As far as I understand your function shouldn't have any parameters.

func greeting() -> (language: String, greeting: String) {

    let language = "English"
    let greeting = "Hello"

    return (language, greeting)
}

This returns a named tuple.

let greet = greeting()
println(greet.language)
println(greet.greeting)
like image 86
Jernej Strasner Avatar answered Jul 17 '26 18:07

Jernej Strasner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!