gpt4 book ai didi

objective-c - Typedef 枚举 Objective-C

转载 作者:行者123 更新时间:2023-12-02 06:43:44 26 4
gpt4 key购买 nike

我有 Distance 类和 typedef enum Unit,

@interface Distance:NSObject{

double m_miles;

}

@property double m_miles;

-(Distance *) initWithDistance: (double) value andUnit:(Unit) unit;

@implementation Distance

-(Distance *)initWithDistance: (double) value andUnit:(Unit) unit{

self = [super init];

if (self){

switch (unit) {

case Unit.miles: m_miles = value;

break;

case Unit.km: m_miles = value/1.609344;

break;
}


}

我在哪里声明我的枚举单位?如何访问

typedef enum{

miles;

km;

}Unit

在另一个类中,我应该可以调用 Distance.Unit.km 或英里:

Distance *a = [[Distance alloc] initWithDistance: 10.2 andUnit: Distance.Unit.km];

最佳答案

在 C 中,enum 不会使其值“合格”。你必须访问它

Distance *a = [[Distance alloc] initWithDistance: 10.2 andUnit:km];
// ^^
// no Distance.Unit.stuff

关于objective-c - Typedef 枚举 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3893731/

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