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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With