views:

1242

answers:

3

Does anyone know how to convert a NSDictionary object into an NSData object as a plist without saving the dictionary first? I would like my iphone app to send an email with a plist containing my dictionary attached. I am currently using skpsmtpmessage, http://code.google.com/p/skpsmtpmessage/, to send email.

Thanks in advance

+2  A: 

You can use NSPropertyListSerialization class for that. Have a look at its method:

+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format
                              errorDescription:(NSString **)errorString

Returns an NSData object containing a given property list in a specified format.

Vladimir
+5  A: 

Use the NSPropertySerialization class; for example, from NSData to NSDictionary:

NSString *errorStr = nil;
NSPropertyListFormat format;
NSDictionary *propertyList = [NSPropertyListSerialization propertyListFromData:data
                                                              mutabilityOption:NSPropertyListImmutable
                                                                        format:&format
                                                              errorDescription:&errorStr];
[errorStr release];

From NSDictionary to NSData, similarly, use the dataFromPropertyList:format:errorDescription: static method. Check out the documentation of the NSPropertySerialization class for more details.

Adrian Kosmaczewski
+4  A: 

Use the NSPropertyListSerialization class. The Dev Centre tutorial on Archives has a section on Serializing Objects which covers this.

Robert Christie