Store enum val in NSDictionary

I have a bunch of JSON objects coming back from the server, and I'm trying to normalize them a bit for my C target.

I have this listing:

//VP_STATUS typedef enum { VP_STATUS_NA, VP_STATUS_STEXP, ... VP_STATUS_COUNT } VP_STATUS; 

I have a function that displays a string in a JSON object (which is NSMutableDictionary) for this enumeration, and then I try to set the status key to NSMUtableDictionary as follows:

 VP_STATUS status = [myStringToEnumMappingFunction:[p objectForKey:@"status_label"]]; [p setValue:status forKey:@"status"]; 

However, on the line setValue: forKey, I get this error:

 "Implicit conversion of VP_STATUS to id is disallowed with ARC" 

Should I first convert the status to another? If so, then this view defeats the purpose of using messages that are defined as VP_STATUS, right?

I'm new to objective-c, so I could have done it completely wrong with everything I know, and I'm open to suggestions.

+4
source share
1 answer

Enumerations as aliases for numbers. The classes in the Objective-C collection can only store references to objects.

The error you get says that the value cannot be converted to id ( id is another way of writing NSObject * ), the object pointer.

If you want to store a value in a collection, you need to wrap it in something like NSNumber . If you use LLVM, you can do this, for example, with

 [p setValue:@(status) forKey:@"status"]; 

And to compare it again, you can return it to a number with:

 VP_STATUS status = [[p valueForKey:@"status"] integerValue]; 
+14
source

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


All Articles