Resolved How do I convert an NSData object to an NSDictionary object, and vice versa?

Discussion in 'iOS Programming' started by moonman239, May 2, 2014.

  1. moonman239, May 2, 2014
    Last edited: May 2, 2014

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Hello everyone,

    In my Core Data model, I have an attribute that is of the NSData type. The data assigned to this attribute is supposed to represent a dictionary.

    I can't use NSString's initWithData method. Nor can I use the JSON object serialization class.
     
  2. dotri84, May 2, 2014
    Last edited by a moderator: May 2, 2014

    dotri84 macrumors newbie

    Joined:
    Jun 27, 2010
    #2
    Why you can't use this ?

    Code:
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
    options:kNilOptions error:&error];
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Because I'm not dealing with JSON data.
     
  4. PBG4 Dude macrumors 68000

    PBG4 Dude

    Joined:
    Jul 6, 2007
    #4
  5. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    Just wanted to add that JSONObjectWithData's return value is supposed to be of type NSString.

    Thank you!
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    No it's not. The returned typename is id:
    Code:
    + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error
    
    The description says "The top level object is an NSArray or NSDictionary."
     
  7. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #7
    Oh, OK. I suppose the reason I got the warning is that I didn't get enough sleep last night.
     

Share This Page