从字符串创建plist文件

时间:2011-09-08 16:37:37

标签: iphone objective-c plist createfile

我有这个字符串:

<!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>

我想创建一个包含此内容的plist文件。我试过这个:

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];

我的控制台显示:

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

但不要创建任何文件。

2 个答案:

答案 0 :(得分:3)

  • 使用error方法的writeToFile:atomically:encoding:error:参数来检索调用返回的错误,这有助于调试

  • 使用NSUTF8StringEncoding代替NSStringEncodingConversionAllowLossy (这不是编码!)

  • 如果可能,首选使用NSPropertyListSerialization类来操作PLIST文件序列化。

  • 您可能会发现阅读Property List Programming Guide

  • 非常有用

答案 1 :(得分:0)

NSStringEncodingConversionAllowLossy不是编码,而是使用类似NSUTF8StringEncoding的内容。