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 trialJonathan Shula
1,993 Pointswhat is the answer to tuples challenge objective 1? i am getting an error: and I don't know how to resolve the problem
swift_lint.swift:11:12: error: '(String, String) -> $T3' is not identical to 'String' return greeting(greeting,language)
I don't know what this error means.
In my playground it states: "Cannot invoke 'greeting' with n argument list of '(String,String)' But because I am trying to return two different strings "language,greeting" I don't know how to have them register as a tuple.
func greeting(person: String) -> (String,String) {
let language = "English"
let greeting = "Hello \(person)"
return greeting(greeting,language)
}
1 Answer
Martin Wildfeuer
Courses Plus Student 11,071 Pointsreturn greeting(greeting,language)
The problem here is the return statement.
This way, you are returning the greeting
function itself, but greeting expects a single String as a parameter. Moreover it expects two Strings to be returned. Having said that, the function is is not what you want to return, but the greeting
and the language
string. Therefore, the return statement should read:
return (greeting,language) // Now you are returning a tuple
Another thing worth mentioning is your returned type -> (String, String)
lets you access the tuple values like this
let test = greeting("Martin")
test.0 // Access first variable "Hello Martin"
test.1 // Access second variable "English"
This works, but it is not very descriptive and might be a bit hard to read later on. By returning a named type -> (greeting: String, language: String)
, you can access the values like this
let test = greeting("Martin")
test.greeting
test.language
So the final could would be something like this
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting,language)
}
Hope that helps!