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

Found challenge task 2 of 2 hard to understand?

Is this question hard to understand or is the reason I am struggling to understand it because I just learnt all the syntax and I'm not confident in what it is asking?

Also two more things, I can't find the error that is not letting me complete the challenge and can anyone provide a simpler method of adding a space in between firstName and lastName please?

Thanks.

structs.swift
struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String{
        let aPerson:String =  firstName + " " + lastName

        return aPerson
    }
}

let fullName = Person(firstName: "Nick", lastName: "F")
fullName.getFullName()

1 Answer

Mit Sengupta
Mit Sengupta
13,823 Points

Hi Nick,

what this question is actually asking is, to use a function that returns a title and your last name. The getFullName() method should return "Dr. F" not your actual full name. Below is how I solved it:

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

}

// Enter your code below class Doctor: Person { var title: String = "Dr." override func getFullName() -> String { return "(title) (lastName)"

}

}

let someDoctor = Doctor(firstName: "Mit", lastName: "Se") someDoctor.getFullName()

Hope this helps!