Write/Read plist in <data> format

Discussion in 'Mac Programming' started by dova, Oct 28, 2009.

  1. macrumors newbie

    Joined:
    Oct 28, 2009
    #1
    hello,

    i would like to create a plist, then write and read <data></data> format for a key in the plist. The rest of keys are in string format.
    The following code does what i want but it writes and reads all key in <string></string> instead of data format.
    How can i get it to work?
    Thanks!

    write:
    PHP:
    /* write plist */
        
    NSString *plistPath = @"/Users/dova/Desktop/data.plist";
        
    NSMutableDictionary *plistDict = [[NSMutableDictionary allocinit];
        [
    plistDict setObject:info forKey:@"name"];
    ...
        [
    plistDict writeToFile:plistPath atomically:YES];
    read:
    PHP:
    /* read plist */
        
    NSString *plistPath = @"/Users/dova/Desktop/data.plist";
        
    NSMutableDictionaryplistDict = [[NSMutableDictionary allocinitWithContentsOfFile:plistPath];
        
    NSData *value;
        
    value = [plistDict objectForKey:@"name"];
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What type of object is info? If it's a NSString then it'll get written as a string. You would need to convert (not cast) to NSData to get what you want.
     
  3. thread starter macrumors newbie

    Joined:
    Oct 28, 2009
    #3
    The info is a string type (I got it to write and read it).
    The next key is idata (setObject:idata forKey:mad:"data") in the string type, and i would like to convert it to data format for both read and write. but i do not know how.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
  5. thread starter macrumors newbie

    Joined:
    Oct 28, 2009
    #5
    i got it to encode in the data format:
    PHP:
    NSData datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
    [
    plistDict setObject:datax forKey:@"data"];
    how is it with decode to read? i do not found any info for decoding in the documentation.
    Thank you
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Are you serious? On the same ***** page I linked to above is this, amazingly obviously named, clearly documented init method.

    In all seriousness: if you couldn't see that give up now.
     
  7. thread starter macrumors newbie

    Joined:
    Oct 28, 2009
    #7
    Thank you!
    i got it to decode:
    PHP:
    NSData *datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
    NSString *dec_data = [[NSString allocinitWithData:datax encoding:NSUTF8StringEncoding]; 
    NSLog (@"decode: %@\n"dec_data);
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    The real question is why? This is not really any more secure than just saving it as a string...
     
  9. thread starter macrumors newbie

    Joined:
    Oct 28, 2009
    #9
    I use the data format to store the encrypted key using openssl. The key is encrypted as plain text:
    Just to make sure that i won't have problem with some weird characters i need to convert it in the data format:
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Ah, OK, well that sounds sensible. I would check/ensure that the encoding you are choosing can represent all the characters you have though...
     

Share This Page