tone10lite
tone10lite

Reputation: 37

Tuples with Swift

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!

Upvotes: 3

Views: 2004

Answers (2)

jwlaughton
jwlaughton

Reputation: 925

I agree that you were trying to set your input variables. This might also be a little more interesting. This function looks up the greeting for a language (there's only one language greeting defined for now):

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

    var greetingDictionary = [String: String]() // Create an empty dictionary
    greetingDictionary["English"] = "Hello"     // Add an object "Hello" for key "English"

    let greeting:String = greetingDictionary["English"]! // set greeting to the greeting for English

    var found = (language, greeting) // Return a tuple

    return found
}

If this is called with:

var greetingFound = greeting("English")
println("In \(greetingFound.0) the greeting is \(greetingFound.1)") // Demonstrate tuple access

it prints this:

In English the greeting is Hello

EDIT:

Oops; my mistake, the function should actually be:

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

    var greetingDictionary = [String: String]() // Create an empty dictionary
    greetingDictionary["English"] = "Hello"     // Add an object "Hello" for key "English"

    let greeting:String = greetingDictionary[language]! // set greeting to the greeting for the language passed in

    var found = (language, greeting) // Return a tuple

    return found
}

Upvotes: 1

Jernej Strasner
Jernej Strasner

Reputation: 4620

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)

Upvotes: 3

Related Questions