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 2.0 challenge 2 of 2

Hello, can someone tell me what is wrong with my code! thank you so much

struct Person {
    let firstName: String
    let lastName: String

func getFullName() -> (String) {
return "\(firstName) \(lastName)"
}

}

let aPerson = Person.init(firstName: "Jenifer", lastName: "Fuentes")
let fullName = aPerson.getFullName()

Seems like everything is OK. However Treehouse, I guess, need the code to be written this way:

class Person {
    let firstName: String
    let lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

    func getFullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

let somePerson = Person(firstName: "Jenifer", lastName: "Fuentes")
somePerson.getFullName()

3 Answers

Get rid of the .init. in front of Person and the parentheses around the return value of the method.

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {
      return "\(firstName) \(lastName)"
    }

}

let aPerson = Person(firstName: "Jenifer", lastName: "Fuentes")
let fullName = aPerson.getFullName()

@Caleb, how come you don't need to init firstName and lastName? Is that because it is a struct?

Yes, exactly. Structs already come with memberwise initializers, meaning that there is a pre-build initializer for the Struct.

Ahh ok, it's just in the documentation I saw a struct initialised.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203

struct Fahrenheit {
    var temperature: Double
    init() {
        temperature = 32.0
    }
}
var f = Fahrenheit()
print("The default temperature is \(f.temperature)° Fahrenheit")
// Prints "The default temperature is 32.0° Fahrenheit"

Caleb Kleveter I have this as my answer as well but it keeps saying: "Bummer! The value being assigned to fullName must be the result of calling the instance method getFullName()" even though this is exactly what I've done. Has this happened to anyone else?

I have this exact answer and I still receive the error: "Bummer! The value being assigned to fullName must be the result of calling the instance method getFullName()"

Quite frustrating : \

Thank you guys =)

Nevermind, I accidentally placed parenthesis around my return type. That's what was causing the error.