gpt4 book ai didi

ios - Objective-c 中的通用枚举类型

转载 作者:行者123 更新时间:2023-12-01 16:21:58 38 4
gpt4 key购买 nike

我有两个objective-c 类HondaDealerShipFordDealerShip .它们都包含相似的属性和方法,所以我想定义一个通用协议(protocol)DealerShip并做一些多态性。

问题是 DealerShip需要包含通用枚举类型属性,以便 HondaDealerShipFordDealerShip可以有不同的concrete枚举类型。

所以我想要这样的东西,

@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

如果我必须在 swift 中执行此操作,我可以使用具有如下关联类型的协议(protocol)
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

我可以在objective-c中做类似的事情吗?

最佳答案

不,您不能在 objective-c 中将枚举与关联类型一起使用。该语言不支持它。

关于ios - Objective-c 中的通用枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55929317/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com