开发者

Objective-C, enumerators and custom setters - How to make it work?

I have an Application Delegate class with a enumeration which looks like this:

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

Additionally I have a property to set a value from the enumerator in my interface file:

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

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

@end

Finally I have the following message in my implementation file:

@implementation Communicator2AppDelegate

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

My issue is the following: the performSelectorOnMainThread message isn't working because it does开发者_Python百科n't accept '_userStatus' as a value. My guess is the message assumes it's an enum, not a real value. I get the following error message upon compilation: "Incompatible type for argument 2 of 'performSelectorOnMainThread:withObject:waitUntilDone.'"

Does anyone have any idea on how to make this work?


You need to pass an object value to this method and enum (that is int) is scalar value. To achieve what you need you must wrap your integer to obj-c object (e.g. NSNumber):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜