Enum with NSnumber

How to create an enumeration containing NSNumber? I have coredata persistence in my application, the object has a status property declared as Integer 16 in coredata, which means it's NSNumber

I would like to declare my listing to contain an NSNumber, not an int, so I can use them without writing this terrible thing.

enum { ERROR, INCOMPLETE, OK } EventStatus; [myObjectOnCoredata setStatus: [[NSNumber alloc] initWithInt:INCOMPLETE]]; 

and just:

 [muObjectOnCoredata setStatus: INCOMPLETE]; 

I think this is possible because I can declare the listing as NSInteger, but it is still not good for me.

thanks for any help

+4
source share
3 answers

Name the status in the managed entity as statusNumber .

Then

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

Just hide the transformations into your implementation.

+12
source

Since last year's WWDC, you can use insertion expressions to create NSNumbers:

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

See also here: http://clang.llvm.org/docs/ObjectiveCLiterals.html
At some point, he says that it is not available in any Apple compiler, but now Clang is the standard one supporting it since version 3.2

Or you add a method to your NSManagedObject subclass using enum EventStatus , which creates a number and calls the original method

+18
source

Enumerations cannot contain objects. Only integer types. You can easily add a value using @ or @ (), but pay attention to the type of the integer type so that it correctly enters. It can be any of signed or unsigned integer types. Failure to decompress correctly can lead to unforeseen overflow due to an implicit conversion with a different length or sign. Although many enumerations use implicit defaults starting from zero, this is not guaranteed.

So, check the definition of the enum and select the correct NSNumber method, such as intValue for int.

0
source

Source: https://habr.com/ru/post/1487746/


All Articles