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 Named Parameters

Rani Shayto
Rani Shayto
769 Points

Adding an Int along with a String to a function

I am trying to add age to the function describing firstName and lastName but it does not seem to work.

func searchName (#firstName: String, #lastName: String, #age: Int) ->(String,Int) {
return "\(firstName) \(lastName) \(age)"
}

I get the error : Could not find member init

Any thoughts on how I can add that?

4 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Rani:

Here try fixing these few things.

1) Your return tuple has only 2 values when you are trying to return 3

2) Name your Elements in the tuple ( not necessary, but recommended )

3) When returning, don’t use string interpolation, instead call the tuple elements direct

If you understand and fix these 3 steps, your code will work. If you don’t understand, let me know and I’ll walk you through.

Good luck

Rani Shayto
Rani Shayto
769 Points

Hi Jhoan,

I managed to get things working after progressing and practicing a little bit. Sorry I had not posted my full code as you asked. Anyhow, thank you for the help! I would have posted what I wrote down now but unfortunately I had deleted the playground.

Hi there,

In your code you are only returning one string:

func searchName (#firstName: String, #lastName: String, #age: Int) -> String {

// this is one string made up of three variables interpolated
return "\(firstName) \(lastName) \(age)"
}

So delete the Int from the return type and this now works fine.

Steve.

Rani Shayto
Rani Shayto
769 Points

Thank you for the help Steve, I now understand what was going on and the function worked once I removed the Int. and kept the string as you suggested.

Rani Shayto
Rani Shayto
769 Points

Thank you Jhoan, I tried something out based on your suggestions and also worked. Here is what I did:

func searchName (#first: String, #last: String, #age: Int) ->(String,String,Int) {
return (first, last, age)
}

This gave me what I was looking for: name(first: "Tom", last: "Ford", age: 25) with (.0 "Tom", .1 "Ford", .2 25) on the right. Have I done what you suggested?

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Rani:

Perfect, remember step two where I said Name your Elements in the tuple ( not necessary, but recommended )?

This will make more clean when creating an instance. Here let me show you.

func searchName (#firstName: String, lastName: String, age: Int) -> (firstName: String,lastName: String,age: Int) {
    return (firstName,lastName,age)
}

// Instance
var names = searchName(firstName: "Rani", lastName: "Smith", age: 25)

names.firstName // will show you Rani
Rani Shayto
Rani Shayto
769 Points

Awesome, I missed that step because I did not understand it well while watching the video, but was pretty straight to the point in your explanation (I guess repeating that video a few times also helped sink in the idea). The only problem I have with your explanation is that you wrote names.firstName while writing that in xcode does not give me anything in return. Instead, what does give me my first name is names.0 (names.01 gives me the lastName and names.02 gives me the age). Did you mean to type in names.0 instead of names.firstName?

Thanks, Rani

Rani Shayto
Rani Shayto
769 Points

I'm sorry, I meant names.1 and names.2 instead of names.01 and names.02 (although they also seem to work)

Jhoan Arango
Jhoan Arango
14,575 Points

Rani, once you name the elements of your tuple, then you can use them when accessing the properties.

// Instead of using 

names.0 

// you can use 

names.firstName

If you want, show me your final code so that I can see what is missing. By the way, you can just reply to me by adding a comment and not by giving an answer. This way, it does not show that your question has a bunch of answers. :)