swift - Unable to change protocol conformance of an object -
in code, wheeled
, vehicle
protocols, bike
class conforms both of protocols
protocol wheeled { var numberofwheels: int { } } protocol vehicle { var maker: string { } var owner: string {get set} var ownerkid: string { } } class bike: vehicle, wheeled { let numberofwheels: int = 0 var ownerkid: string = "junior" var maker: string { return "ford" } var owner: string { { return "bob" } set { ownerkid = "\(newvalue) junior" } } } let bike: bike = bike() var thebike: vehicle = bike // #1 var thebike: wheeled = bike // #2 error: invalid redeclaration of 'thebike'
when check properties of thebike
, in #1, thebike
object has properties conform vehicle
protocol; while in #2 thebike
object has properties conform wheeled
properties
therefore, feel thebike
in #1 , #2 different, why tells me invalid redeclaration?
question: how should change protocol conformance of object? or allowed change conformance of object?
appreciate time , help.
- you cannot declare variable (i.e. same name) twice.
- if need access properties of protocol, use
if let ... as? ...
recast.
example:
if let thebike = thebike as? wheeled { print(thebike.numberofwheels) }