0

I am having an issue where NSDictionary is changing the values inside the <data> tag when I read the file in from disk. It also changes it when I save it back to disk.

I have a .plist file on disk that I read into a dictionary. This is the contents of the original ,plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Email</key>
    <string>user@email.com</string>
    <key>Name</key>
    <string>User</string>
    <key>Signature</key>
    <data>
    RBMIIr2dotiiKH5p6oTGsjqR7FSsmafZHpmJ7coO56eY8d5mBiVSZI5LCe7I3vGJk7WX
    0W/dLyAS+Es5fPlDhhSLNzesR1lNzMaTdfEKGU1FkvLE8bsZFJ7pK5gFc62e1NfP2eyU
    59SwWZDiqXumhUIOebhdZk5amtY7mN/McYM=
    </data>
</dict>
</plist>

I need the value of <data> so I can display it in an NSTextField, so I get the value like this:

NSData *key = [[NSDictionary dictionaryWithContentsOfFile:fileURLString] objectForKey:@"Signature"];

Now when I try to display the value of the key using NSLog(@"key: %@", key); it shows me this:

<44130822 bd9da2d8 a2287e69 ea84c6b2 3a91ec54 ac99a7d9 1e9989ed ca0ee7a7 98f1de66 06255264 8e4b09ee c8def189 93b597d1 6fdd2f20 12f84b39 7cf94386 148b3737 ac47594d ccc69375 f10a194d 4592f2c4 f1bb1914 9ee92b98 0573ad9e d4d7cfd9 ec94e7d4 b05990e2 a97ba685 420e79b8 5d664e5a 9ad63b98 dfcc7183>

So, how can I get the original <data> as an NSString in the same format that it shows in the .plist? and what is this (wrong) format directly above called?

NOTE: I have managed to get/store the original <data> value by extracting the text using componentsSeparatedByString, but it seems like a bad hack.

Now I need to save the .plist file back to disk so it matches the original .plist file, and that doesn't work either. Here's what I am doing:

NSString *sig = @"RBMIIr2dotiiKH5p6oTGsjqR7FSsmafZHpmJ7coO56eY8d5mBiVSZI5LCe7I3vGJk7WX0W/dLyAS+Es5fPlDhhSLNzesR1lNzMaTdfEKGU1FkvLE8bsZFJ7pK5gFc62e1NfP2eyU59SwWZDiqXumhUIOebhdZk5amtY7mN/McYM=";
const char *cString = [sig cStringUsingEncoding:NSASCIIStringEncoding];
NSData *d = [NSData dataWithBytes:cString length:strlen(cString)];
NSMutableDictionary *nameDetails=[[NSMutableDictionary alloc] init]; 
[nameDetails setValue:email forKey:@"Email"]; 
[nameDetails setValue:name forKey:@"Name"]; 
[nameDetails setValue:d forKey:@"Signature"]; 

...

[nameDetails writeToFile:fileURLString atomically:YES];

When I look at the new .plist file, the value of <data> is wrong. It should be the same as the value I passed for Signature key. This is the resulting (bad) .plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Email</key>
    <string>user@email.com</string>
    <key>Name</key>
    <string>User</string>
    <key>Signature</key>
    <data>
    UkJNSUlyMmRvdGlpS0g1cDZvVEdzanFSN0ZTc21hZlpIcG1KN2NvTzU2ZVk4ZDVtQmlW
    U1pJNUxDZTdJM3ZHSms3V1gwVy9kTHlBUytFczVmUGxEaGhTTE56ZXNSMWxOek1hVGRm
    RUtHVTFGa3ZMRThic1pGSjdwSzVnRmM2MmUxTmZQMmV5VTU5U3dXWkRpcVh1bWhVSU9l
    YmhkWms1YW10WTdtTi9NY1lNPQ==
    </data>
</dict>
</plist>

So, why does the value of Signature data, change when I save it back to a .plist?

RGB World
  • 399
  • 1
  • 6
  • 19

1 Answers1

1

The value is the same. It's just the representation that differs, just as 10, 10.000 and 0x0A are different representations of the same value. The XML property list stores the value with a Base64 encoding, while -[NSData description] (as printed by NSLog()) yields with a base 16 (a. k. a. hexadecimal) encoding. You're looking for a way to Base64-encode an NSData object.

Community
  • 1
  • 1
gcbrueckmann
  • 2,413
  • 18
  • 10