Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift 2.0 Complex Data Structures Adding Instance Methods

How do you solve this?????

On XCode, what I put is running fine? Am I missing something?

structs.swift
struct Person {
    let firstName: String
    let lastName: String
        func getFullName(name: Person) -> String {
        var fullName = "\(firstName) \(lastName)"
        return fullName
        }
}

let keithJames = Person(firstName: "Keith", lastName: "James")

keithJames.getFullName(keithJames)

2 Answers

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)"
    }
}

Two problems, the first is, you're trying to call an instance on the name when it only asks you to create a method (This is ok, but not needed). The second is you do not need a variable as shown in my code which passes. You would want to call on the "return" method since you you specified with "-> String" that this method is to return something. Last but not least, you did not initialize "firstName" and "lastName" as strings.

Josh Reynolds
Josh Reynolds
10,734 Points

Hey Keith,

You're pretty much on the right track, instead of saving the first and last name in a var, you can just write it out like below. Also the example asks you to assign the instance created to a constant named aPerson however not keithJames. Then call the instance method and assign the full name to a constant named fullName.

    struct Person {
        let firstName: String
        let lastName: String

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

    let aPerson = Person(firstName: "Keith", lastName: "James")
    let fullName = aPerson.getFullName()