Objective-C,枚举器和自定义setter - 如何使其工作?

时间:2010-01-22 09:25:38

标签: objective-c properties types enums

我有一个Application Delegate类,其枚举如下所示:

typedef enum {
    Online = 3500,
    DoNotDisturb = 9500,
    Offline = 18500,
    Away = 15500,
    Busy = 6500,
    BeRightBack = 12500
} status;

此外,我有一个属性可以从我的接口文件中的枚举器中设置一个值:

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    status userStatus;
}

@property (nonatomic, setter=setStatus) status userStatus;

@end

最后,我的实现文件中有以下消息:

@implementation Communicator2AppDelegate

- (void)setStatus:(status)_userStatus {
    if ([NSThread isMainThread]) {
        // some stuff happens here ...
    } else {
        [self performSelectorOnMainThread:@selector(setStatus:) withObject:_userStatus waitUntilDone:NO];
    }
}

我的问题如下:performSelectorOnMainThread消息无效,因为它不接受'_userStatus'作为值。我的猜测是消息假设它是枚举,而不是真正的价值。编译时出现以下错误消息:“performSelectorOnMainThread:withObject:waitUntilDone。”的参数2的不兼容类型。

有没有人知道如何使这项工作?

1 个答案:

答案 0 :(得分:1)

您需要将对象值传递给此方法,枚举(即int)是标量值。要实现所需,必须将整数包装到obj-c对象(例如NSNumber):

- (void)setStatus:(status)_userStatus {
    if ([NSThread isMainThread]) {
        // some stuff happens here ...
    } else {
        [self performSelectorOnMainThread:@selector(setStatus:) withObject:[NSNumber numberWithInt:_userStatus] waitUntilDone:NO];
    }
}