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!

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

Peter Fuhrey
Peter Fuhrey
2,146 Points

Problem concatenating two string parameters in swift.

Can somebody explain to me why i'm getting "Use of unresolved identifier" in this code? I thought that I was able to pass whatever I liked into the string and out came my answer.

func fullName(firstName: String, lastName: String) -> String { var myName = "(firstName) (lastName)" return myName }

let myName = fullName(Peter, Fuhrey)

I apologize in advance if anything is unclear or just flat out doesn't make sense. I'm very new to programming.

1 Answer

Hey Peter, there's no need to apologize since your question is a completely valid one. You're right when you say that you could use your function fullName to pass two Strings and have a new one returned. The reason you're getting your error is for easily fixable syntax errors within your code. When you invoke the fullName function, you need to provide it with two strings as its parameters in order for it to return you a new String as you have designated in the function declaration here:

func fullName(firstName:String, lastName:String) -> String

It's important to remember that Strings are always denoted by quotation marks so when you call your method, it should look like this:

let myName = fullName("Peter",  "Fuhrey")

Also remember that if you want to use a variable directly in a string, you must denote so using a '\' character like this:

var myName = "\(firstName) \(lastName)"

I hope this helps, Swift is a really fun language, and with a little practice, you'll definitely get the hang of it. Good luck

Peter Fuhrey
Peter Fuhrey
2,146 Points

Lol I feel foolish for not noticing that xD. Thank you very much though!