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

Adding 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

You 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()

Hi 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
Shivi Jaiswal
3,398 Points

This 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
Anthia Tillbury
3,388 Points

I'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()

Thank 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.