【问题标题】:Reading from and writing to plists in xcode 4.0.2 - issue retrieving from dictionary在 xcode 4.0.2 中读取和写入 plist - 从字典中检索问题
【发布时间】: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"];

【问题讨论】:

    标签: iphone ios plist


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      相关资源
      最近更新 更多