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 Object-Oriented Swift Complex Data Structures Adding Instance Methods

Sean Lafferty
Sean Lafferty
3,029 Points

Slightly confused whether I use concatenation here or not! Some guidance please!

Hi guys, I feel like Ive actually gained a good grasp on structs thru these videos however this section I had a quick coffee break and came back, now IM a bit confused! Some advice on what im doing wrong here would be great! Thanks, Sean

structs.swift
struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> [String] {

    return (\(firstName) " " \(lastName))

    }
}

1 Answer

Garrett Votaw
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Garrett Votaw
iOS Development Techdegree Graduate 15,223 Points

Hey Sean,

You're pretty close here. It looks like you are trying to concatenate AND also interpolate the string.

Concatenation looks like this

let string = "bob" + " " + "ate" + " " + "apples" 

// OR

let string = firstName + " " + lastName //<-- Both those values are strings already so we can just add them together with a space in between

Interpolation looks like this

let string = "\(somePerson) ate apples"

with interpolation you are inserting a variable INSIDE a string.

For your issue here compare your answer with the following

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> [String] {

    return ("\(firstName) \(lastName)")

    }
}

Here I am just inserting two constants into a string.

Personally I like interpolation 99.999% of the time because it is faster/easier for me to type. I would definitely recommend you practice it as it can make your code way more concise in the future!