【发布时间】:2011-08-31 22:53:46
【问题描述】:
我一直在关注这个Tutorial 关于如何从 plist 中读取。但是我无法读取带有键名的字符串。我尝试输入NSLog("%@", [temp objectForKey:@"Name"]);,但它返回null。我有一个类似的问题,之前涉及打开 xml 文件并手动将 dict 更改为数组。我该如何解决这个问题?
xml 文件中的层次结构让我感到困惑。我用字符串 John Doe 创建的名为 root 的字典是字典本身的一部分吗?澄清感谢!
来自上面链接的教程:
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
NSLog(@"%@", [temp objectForKey:@"Name"];
【问题讨论】: