I want to ensure by compiler that CarViewController
only receives a Car
in the vehicle property.
Given the following swift example code:
class Vehicle {
func doSomething(){}
}
class Car: Vehicle {
func doCarThings(){}
}
class VehicleViewController : UIViewController {
var vehicle : Vehicle!;
override func viewDidLoad() {
super.viewDidLoad();
vehicle.doSomething();
}
}
class CarViewController:VehicleViewController {
var vehicle: Car!
override func viewDidLoad() {
super.viewDidLoad();
vehicle.doCarThings();
}
}
I get the following error: Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'
I tried with a generics-based approach:
class Vehicle {
func doSomething(){}
}
class Car: Vehicle {
func doCarThings(){}
}
class VehicleViewController<T:Vehicle> : UIViewController {
var vehicle : T!;
override func viewDidLoad() {
super.viewDidLoad();
vehicle.doSomething();
}
}
class CarViewController:VehicleViewController<Car> {
override func viewDidLoad() {
super.viewDidLoad();
vehicle.doCarThings();
}
}
It is correct but using generics in storyboard classes results in errors (since they get compiled to objective-c).
How can I do this without using generics?
Thanks!