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
Carmen Ibarcena
iOS Development Techdegree Student 8,296 PointsObject-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()
3 Answers
Caleb Kleveter
Treehouse Moderator 37,862 PointsGet 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()
Andrew Taylor
11,500 Points@Caleb, how come you don't need to init firstName and lastName? Is that because it is a struct?
Caleb Kleveter
Treehouse Moderator 37,862 PointsYes, exactly. Structs already come with memberwise initializers, meaning that there is a pre-build initializer for the Struct.
Andrew Taylor
11,500 PointsAhh ok, it's just in the documentation I saw a struct initialised.
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"
Jessica Hunter
Full Stack JavaScript Techdegree Student 13,097 PointsCaleb 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?
Nick Sieber
iOS Development Techdegree Student 3,121 PointsI 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 : \
Carmen Ibarcena
iOS Development Techdegree Student 8,296 PointsThank you guys =)
Jessica Hunter
Full Stack JavaScript Techdegree Student 13,097 PointsNevermind, I accidentally placed parenthesis around my return type. That's what was causing the error.
Anton Melekh
2,729 PointsAnton Melekh
2,729 PointsSeems like everything is OK. However Treehouse, I guess, need the code to be written this way: