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

Zachary Goforth
Zachary Goforth
748 Points

On Challenge Task 3 how do I use the println function to print the value 'language' for the 'result' tuple?

I know this is a easy task, I'm just having trouble finding an answer.

Here is my guess for the 3rd task... println(result: language)

Obviously I'm using the println function but it's the second part of the 3rd task that i don't understand. Any help is appreciated. :)

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

          return (greeting ,language)
           }

var result = greeting("Tom") 

1 Answer

When returning tuples with named elements, you can access them using dot notation.

So your print statement should be println(result.language)

Every time you put a colon somewhere it is because you are declaring something - typically the type of a variable/constant/argument (like language: String) - you never use colon to access the values.

Zachary Goforth
Zachary Goforth
748 Points

I used the println(result.language) and this error showed up...

Bummer! You printed "Hello Tom". It should have printed "English".

I haven't changed my code, I just added the println function.

Well that's unfortunate - but I can tell you, that the code is correct. There are only two possible scenarios:

  1. The code challenge engine doesn't know good code when it sees it (I'm obviously joking a bit, but it does behave strangely from time to time)
  2. You actually DID change your code, just a little, and the order of the elements in your return statement doesn't match your definition
Zachary Goforth
Zachary Goforth
748 Points

Thanks for the help anyway.