在xcode 4.0.2中读取和写入plists - 从字典中检索问题

时间:2011-08-31 22:53:47

标签: iphone ios plist

我一直在关注如何从plist中读取Tutorial 。但是我无法读取带有键名的字符串。我尝试放NSLog("%@", [temp objectForKey:@"Name"]);但它返回null。我有一个类似的问题,之前涉及打开xml文件并手动将dict更改为数组。我该如何解决这个问题?

xml文件中的层次结构让我感到困惑。我创建的字典名为root,字符串John Doe是字典本身的一部分?澄清赞赏!

从上面链接的教程:

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

1 个答案:

答案 0 :(得分:1)

这很可能是因为没有对象@"Name"(请记住,Xcode区分大小写!)在(推测)NSDictionary名为temp的对象中。

要获得更具体的答案,您需要在定义temp的位置发布代码。