我意识到(正如我的更新所暗示的那样)我可以使用NSPropertyListSerialization类,因为NSData
返回的-dataWithPropertyList:format:options:error:
只是一个 UTF-8 字符串。这是我用来序列化的:
NSData *data = value;
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:data
format:NSPropertyListXMLFormat_v1_0
options:0
error:&error];
if (error) {
NSLog(@"Error serializing data to plist XML: %@", error);
} else {
NSString *plistString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];
NSXMLElement *dataElement = [NSXMLElement elementWithName:field
stringValue:plistString];
}
并反序列化:
NSData *plistData = [element.stringValue dataUsingEncoding:NSUTF8StringEncoding];
NSData *originalData = [NSPropertyListSerialization propertyListWithData:plistData
options:NSPropertyListImmutable
format:NULL
error:&error];
if (error) {
NSLog(@"Error deserializing data from plist XML: %@", error);
} else {
value = originalData;
}