0

I am trying to store an enum value in coredata, though I understand the way doing it with normal coredata. My case is specific to AFIncrementalStore.

I am trying to do the following

    - (NSDictionary *)attributesForRepresentation:(NSDictionary *)representation
                                         ofEntity:(NSEntityDescription *)entity
                                     fromResponse:(NSHTTPURLResponse *)response
    {
      ......
      if (representation[@"type"] != nil) {
         [mutablePropertyValues setValue:[NSNumber numberWithInt:(MyEnumType)[Content typeFromString:representation[@"type"]]] forKey:@"contentType"];
      }
      .....

    }

Though I am able to save this as an NSNumber, how can I retrieve the enum value from my entity. Since, AFIncrementalStore is taking care of storing the value to entity, I am not sure how to retrieve the enum value. Can anyone Please help. Thanks in advance.

EDIT: My problem is while retreiving I will have access to the entity which has the contentType as enum not as NSNumber For eg. I cant do [myentity.contentType intValue], since myentity.contentType is already a enum or integer value.

jaishankar
  • 249
  • 1
  • 4
  • 13
  • I'm not sure if I'm misunderstanding your question but I think what you are looking for is `[myNSNumberValue intValue]`, which will get an int from an NSNumber. – progressiveCavemen Jun 20 '13 at 18:09
  • jaishankar, That doesn't seem correct. NSManagedObject attributes can't be primitives afaik. If it is truly an int, then myEntity.contentType will give you what you are looking for. If @Anthony Lawrence's solution doesn't work and neither does myEntity.contentType, then there's a different problem. Also, how is this specific to AFIncrementalStore? – geraldWilliam Jun 20 '13 at 18:41
  • @geraldwilliam i thought it wud be possible after reading this http://stackoverflow.com/questions/1624297/best-way-to-implement-enums-with-core-data see answer by Daniel eggert, correct me if i am missing something – jaishankar Jun 20 '13 at 19:21
  • If you look closely, Daniel's setter makes an NSNumber of the int value representing the enum and his getter extracts that int value. The attribute on the NSManagedObject is stored as an NSNumber. – geraldWilliam Jun 20 '13 at 19:28
  • What result do you get when you use [myentity.contentType intValue]? Any result? – geraldWilliam Jun 20 '13 at 19:28
  • some garbage value -12798, 2058 like that. – jaishankar Jun 20 '13 at 20:09

0 Answers0