I have two objective-c classes HondaDealerShip
and FordDealerShip
. They both contain similar properties and methods, so I want to define a common protocol DealerShip
and do some polymorphism.
The problem is this DealerShip
needs to contain generic enum type properties, so that HondaDealerShip
and FordDealerShip
can have different concrete
enum types.
So I want something like this,
@protocol DealerShip
@property (nonatomic, readonly) enum location;
-(void)printPriceOfModel:(enum)vehicleModel;
@end
typedef NS_ENUM(NSInteger, HondaLocation) {
HondaLocationSouthEast,
HondaLocationNorthWest
}
typedef NS_ENUM(NSInteger, HondaModel) {
Accord,
Civic
}
@interface HondaDealerShip: NSObject<DealerShip>
@property (nonatomic, readonly) HondaLocation location;
- (void)printPriceOfModel:(HondaModel)vehicleModel {
//print price here
}
@end
typedef NS_ENUM(NSInteger, FordLocation) {
FordLocationEast,
FordLocationWest
}
typedef NS_ENUM(NSInteger, FordModel) {
Mustang,
Focus
}
@interface FordDealerShip: NSObject<DealerShip>
@property (nonatomic, readonly) FordLocation location;
- (void)printPriceOfModel:(FordModel)vehicleModel {
//print price here
}
@end
If I have to do this in swift
, I could use protocols with associated types like below
protocol DealerShip {
associatedtype Location
associatedtype Model
var location: Location { get }
func printPriceOfModel(model : Model)
}
enum HondaLocation: Int {
case sountEast
case northWest
}
enum HondaModel: Int {
case accord
case civic
}
struct HondaDealerShip: DealerShip {
var location: HondaLocation
func printPriceOfModel(model: HondaModel) {
//print
}
}
//same for FordDealerShip
can I do similar in objective-c?.
Nope, you cannot use enum with associated types in objective c. The language does not support it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.