Create plist file from string
I have this string:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Secciones</key>
<array>
<dict>
<key>Uid</key>
<integer>1</integer>
<key>DateInsert</key>
<date>3911-04-15T09:20:40Z</date>
<key>DateModify</key>
<date>3911-04-15T09:20:40Z</date>
<key>Name</key>
<string>Seccion 1</string>
<key>Description</key>
<string>Esta es la sección 1.</string>
</dict>
<dict>
<key>Uid</key>
<integer>2</integer>
<key>DateInsert</key>
<date>3911-04-15T09:20:40Z</date>
<key>DateModify</key>
<date>3911-04-15T09:20:40Z</date>
<key>Name</key>
<string>Seccion 2</string>
<key>Description</key>
<string>Esta es la sección 2.</string>
</dict>
<dict>
<key>Uid</key>
<integer>3</integer>
<key>DateInsert</key>
<date>3911-04-15T09:20:40Z</date>
<key>DateModify</key>
<date>3911-04-15T09:20:40Z</date>
<key>Name</key>
<string>Seccion 3</string>
<key>Description</key>
<string>Esta es la sección 3.</string>
</dict>
</array>
</dict>
</plist>
I want to create a plist file with this content. I have tried with this:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent: @"开发者_高级运维sec.plist"];
NSLog(@"%@",documentsDirectory);
NSLog(@"%@",fileName);
//n is my string
[n writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
my console show:
2011-09-08 18:26:59.999 Catalogo-V1[1071:207] /Users/.../Library/Application Support/iPhone Simulator/4.3/Applications/6D96FC16-D6CB-4A03-A0F5-38A0C0C473C7/Documents
2011-09-08 18:27:00.000 Catalogo-V1[1071:207] /Users/.../Library/Application Support/iPhone Simulator/4.3/Applications/6D96FC16-D6CB-4A03-A0F5-38A0C0C473C7/Documents/sec.plist
But don´t create any file.
Use the
error
parameter ofwriteToFile:atomically:encoding:error:
method to retreive the error returned by the call, this will help debugUse
NSUTF8StringEncoding
instead ofNSStringEncodingConversionAllowLossy
(which is not eve an encoding!)Prefer using the
NSPropertyListSerialization
class to manipulate PLIST files serialization if possible.You may find useful to read the Property List Programming Guide too
NSStringEncodingConversionAllowLossy
is not an encoding, use something like NSUTF8StringEncoding
instead.
精彩评论