Objective c 在.plist上写入数组

Objective c 在.plist上写入数组,objective-c,Objective C,我有个问题。我有这个文件字典。plist: <plist version="1.0"> <dict> <key>2</key> <array> <string>a</string> <string>b</string> <string>c</string> </array> <

我有个问题。我有这个文件字典。plist:

<plist version="1.0">
<dict>
    <key>2</key>
    <array>
        <string>a</string>
        <string>b</string>
        <string>c</string>
    </array>
</dict>
</plist>
我想把它写在字典上。plist我试着:

[contentArray writeToFile:plistPath atomically:YES];

[contentArray release];
但是这个代码不起作用,我收到一个错误,我该怎么做


谢谢

因为您没有提供错误,我想问题可能是

NSMutableDictionary * contentArray = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
仅创建可变容器(您可以更改contentArray的内容,但不能更改其中的对象(叶)

如果需要可变叶,请改用NSPropertyListSerialization:

NSMutableDictionary * contentArray = [NSPropertyListSerialization propertyListFromData: [NSData dataWithContentsOfFile: plistPath] mutabilityOption: NSPropertyListMutableContainersAndLeaves format: nil errorDescription: nil];

谢谢IRaivis,不,它以这种方式工作很好,我以正确的模式读取plist,我无法写入它,因此您认为这取决于我如何读取文件?此文件是否存储在资源包或文档文件夹中?我的理解是,您不能更改存储在资源包中的文件,因为它是“安装”文件的一部分
NSMutableDictionary * contentArray = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
NSMutableDictionary * contentArray = [NSPropertyListSerialization propertyListFromData: [NSData dataWithContentsOfFile: plistPath] mutabilityOption: NSPropertyListMutableContainersAndLeaves format: nil errorDescription: nil];