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

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