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 Protocols in Swift Protocol Basics Modeling Behavior With Protocols

Why does initializing startDate "self.startdate = startDate" give an error saying assigning a property to itself?

In the video, there are no errors when Pasan writes this, but in my playground there is an error

Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

Would you be able to provide your code ?

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Fredrick,

At a first glance, I do not see the parameter in the initializers signature named "startDate". With that said, the error is showing because you are initializing "startDate" from it's own property.

To fix this, add the parameter "startDate" to the initializer. ( change the name from date to startDate )

init(name: String, address: String, startDate: Date, type: EmployeeType) {

    self.name = name
    self.address = address
    self.startDate = startDate
    self.type = type

}

OR if you want to use "date", you have to initialize it this way.

init(name: String, address: String, date: Date, type: EmployeeType) {
    self.name = name
    self.address = address
    startDate = date // Note that "self" is no longer required
    self.type = type
}

// Self is not required because there is no ambiguity 
// between the parameter name and the property

Hope this helps.

Thank you, this helped so much

class Employee { let name: String let address:String let startDate: Date let type : EmployeeType

init(name: String, address: String, date: Date, type: EmployeeType) {
    self.name = name
    self.address = address
    self.startDate = startDate
    self.type = type
}

}