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 trialGeorge Houghton
3,315 PointsAdding an Instance Method
I'm attempting to solve Challenge Task One in Lesson 4 of Object Oriented Swift 2.0. I'm using the following code to combine the first and last names. The code appears to be working fine in my Playground, but I keep being told that there is an error.
struct Person { let firstName: String let lastName: String
func getFullName(firstName: String, lastName: String) -> String {
let fullName = "\(firstName) \(lastName)"
return(fullName)
}
}
Any thoughts what I'm missing?
George H.
5 Answers
Ali Saif
14,567 PointsYou do not need to specify the inputs to the func as it is already being referenced to the struct. For example: struct Person { let firstName: String let lastName: String
func getFullName()->String{
return "\(firstName) \(lastName)"
}
} let aPerson = Person(firstName:"abc",lastName:"cde") let fullName = aPerson.getFullName()
Andrew Eldon
2,620 PointsHi there,
I don't know if you solved this but this worked for me: ''' struct Person { let firstName: String let lastName: String
func getFullName() -> String {
let fullName = "\(firstName) \(lastName)"
return fullName
}
} '''
Shivi Jaiswal
3,398 PointsThis is the correct code which is working for Treehouse Code Challenge!!
struct Person {
let firstName: String
let lastName: String
func getFullName()->String{
let fullName : String = (firstName + " "+lastName)
return (fullName)
}
}
Anthia Tillbury
3,388 PointsI'm using Xcode Version 8.2.1 (8C1002) and the following is "not compiling" in TreeHouse!
struct Person {
let firstName: String
let lastName: String
func fullName() -> String {
let name = "\(firstName) \(lastName)"
return (name)
}
}
let aPerson = Person.init(firstName: "Master", lastName: "Bates")
let myFullName = aPerson
myFullName.fullName()
George Houghton
3,315 PointsThank you for your help. I see how I can shorten the code in the example I gave. Unfortunately, I am still uncertain why the code is not working in their Code Challenge! Here is the Code Challenge in it's entirety.
"Challenge Task 1 of 2 (Complex Data Structures - Adding Instance Methods)
Given the struct below in the editor, we want to add a method that returns the personβs full name. Declare a method named getFullName() that returns a string containing the personβs full name. Note: Make sure to allow for a space between the first and last name"
struct Person { let firstName: String let lastName: String
}
Here is my response (which works in my Playground, but not in their challenge)
struct Person { let firstName: String let lastName: String
func getFullName(firstName firstName: String, lastName: String) -> String {
let fullName = "\(firstName) \(lastName)"
return(fullName)
}
}
George H.