Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum with NSnumber

How do I create an enum that contains NSNumber ?? i have a coredata persistence in my application, an entity has a status property declared as Integer 16 in coredata that means it is an NSNumber

I would love to be able to declare my enum in order to contain NSNumber and not int so i can use them without having to write this awful thing

enum {
  ERROR,
  INCOMPLETE,
  OK
} EventStatus;

[myObjectOnCoredata setStatus: [[NSNumber alloc] initWithInt:INCOMPLETE]];

and just:

[muObjectOnCoredata setStatus: INCOMPLETE];

I think it shoul be possible since I can declare the enum as NSInteger, but it still is not good for me

thanks for any help

like image 548
Zerho Avatar asked Nov 28 '22 07:11

Zerho


2 Answers

Since last year WWDC you can use boxed expressions to create NSNumbers:

@(INCOMPLETE) // Equals to [NSNumber numberWithInt:INCOMPLETE]

See also here: http://clang.llvm.org/docs/ObjectiveCLiterals.html
At one point it says it's not available in any Apple compiler but now Clang is the default one which supports this since version 3.2

Or you add a method to your subclass of NSManagedObject taking an enum EventStatus which creates a number and calls the original method

like image 175
JDS Avatar answered Dec 15 '22 08:12

JDS


Name the status on the managedObject as statusNumber.

Then

@dynamic statusNumber


- (void)setStatus:(EventStatus)status {
    self.statusNumber = [[NSNumber alloc] initWithInt:status]];
}

- (EventStatus)status {
    return [self.statusNumber intValue];
}

Just hide the conversions into your implementation.

like image 24
Sulthan Avatar answered Dec 15 '22 08:12

Sulthan