Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

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 doesn'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?

Upvotes: 0

Views: 911

Answers (1)

Vladimir
Vladimir

Reputation: 170859

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];
    }
}

Upvotes: 1

Related Questions