如何阅读和解析一个plist

时间:2013-12-27 20:13:29

标签: ios nsdictionary plist

我喜欢阅读和解析plist文件,但它无法正常工作。

这是我的代码:NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:self.plistPath]; dict总是(null)

我尝试将内容作为字符串,这是我的代码:

NSString *string = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:self.plistPath] encoding:NSUTF8StringEncoding];

self.plistPath is /tmp/com.apple.audio.hogmode.plist

这是plist的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

想出来了!

NSString *error;
NSPropertyListFormat format;
NSDictionary *dict = [NSPropertyListSerialization propertyListFromData:[NSData dataWithContentsOfFile:self.plistPath] mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];