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 Class Inheritance Overriding Methods

Gibson Smiley
Gibson Smiley
1,923 Points

Using "Dr." in place of firstName

No errors when I type my code out in Xcode but I'm getting an error here. Honestly, I'm just lost for what I need to do.

My code:

class Doctor: Person {

override init(firstName: String, lastName: String) {
    super.init(firstName: "Dr.", lastName: <#T##String#>)
}

}

let someDoctor = Doctor(firstName: "Gibson", lastName: "Smiley")

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

They don't want you to override the init method, they want you to override the getFullName method. In your version, you're saying their firstName will default to "Dr". But they're still going to have a real first name, we're just defining the formal way they're addressed. The name of the getFullName method might be a bit misleading.

So this is how we would override the getFullName method instead:

class Doctor: Person {
    override func getFullName() -> String {
        return "Dr. \(lastName)"
    }
}

let someDoctor = Doctor(firstName:"Sam", lastName: "Smith")