【问题标题】:Problem with fetching dictionary objects in array from plist从 plist 中获取数组中的字典对象的问题
【发布时间】:2010-05-12 07:40:58
【问题描述】:

您使用什么数据类型来获取 plist 中类型为字典的项目,即 nsmutabledictionary 或 nsdictionary?因为我正在使用以下代码从 plist 中的字典数组中检索字典对象。

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; //APP CRASHES HERE

NSLog(@"MYDICT : %@",_myDict);
NSString *myKey = (NSString *)[_myDict valueForKey:@"Contents"] ; 

[[cell lblFeed] setText:[NSString stringWithFormat:@"%@",myKey]];

在这里,第一行显示 objc_msgsend。 ContentArray 是一个 nsarray,它的内容显示了 plist 中的 2 个对象。在 plist 中,它们是字典对象。那为什么会出现这个错误呢?

编辑

基本上我的console中contentArray的内容如下图:

CONTENT ARRAY :  
(
    {
    favourites = 0;
    id = 0;
    story = "This is my first record";
    timestamp = 324567;
},
    {
    favourites = 0;
    id = 1;
    story = "This is my second record";
    timestamp = 321456;
}
)

我想从内容数组中检索这些字典对象。

【问题讨论】:

  • 你的 .plist 的具体内容是什么(不要只是总结)?确切的错误信息是什么?如果错误导致您的应用崩溃,它会在哪一行崩溃?
  • plist 内容如上。我收到 objc_msgsend 消息,控制台中没有任何内容。应用程序在上述行崩溃。
  • 那里的“故事”是什么?什么是“根”标签? PList 有相当严格的语法。数组是“数组”,字典使用“dict”标签等。
  • 您可以在这里看到带有字典和数组的 plist 示例:developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/…

标签: iphone plist


【解决方案1】:

NS 字典。你不能简单地说

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; 

希望它现在是一个可变字典。它仍然是一个正常的不可变的dictionary。所以,你应该这样写:

NSMutableDictionary *_myDict = [NSMutableDictionary dictionaryWithDictionary:[contentArray objectAtIndex:0]];

这将从 plist 中的字典创建可变字典。

您可以在“属性列表编程指南”中阅读它,http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/index.html

更新:

你还有一个奇怪的 plist 内容。此处提到了可用的 xml-plist 类型: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html#//apple_ref/doc/uid/10000048i-CH3-SW1

这里描述了整个 xml-plist 结构: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html#//apple_ref/doc/uid/10000048i-CH6-SW1

工作代码

void test() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *arrayIWillWrite = [NSMutableArray array];
    NSMutableDictionary *dictionary;

    dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"favourites"];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"id"];
    [dictionary setObject:@"This is my first record" forKey:@"story"];
    [dictionary setObject:[NSNumber numberWithInt:324567] forKey:@"timestamp"];
    [arrayIWillWrite addObject:dictionary];

    dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"favourites"];
    [dictionary setObject:[NSNumber numberWithInt:1] forKey:@"id"];
    [dictionary setObject:@"This is my second record" forKey:@"story"];
    [dictionary setObject:[NSNumber numberWithInt:321456] forKey:@"timestamp"];
    [arrayIWillWrite addObject:dictionary];

    [arrayIWillWrite writeToFile:@"/Users/alex/test.plist" atomically:NO];

    NSArray *arrayThatWasRead = [NSArray arrayWithContentsOfFile:@"/Users/alex/test.plist"];
    NSLog(@"%@", arrayThatWasRead);

    NSDictionary *dictionaryFromArrayThatWasRead = [arrayThatWasRead objectAtIndex:0];
    NSLog(@"%@", dictionaryFromArrayThatWasRead);

    [pool release];
}

【讨论】:

  • Alexander Babaev,我尝试按照您的建议进行操作,但没有成功,它仍然在同一行显示 objc_msgsend。我想从 nsarray 中获取第一个字典对象。
  • 你能显示它显示给你的确切信息吗?什么消息是错误的,它被发送到什么对象?
  • 它显示了 objec_msgsend。控制台中什么都没有。在这里,我正在创建一个 nsdictionaries 数组。那样可以么?或者它应该是一个数组字典?
  • 您可以查看我在答案中插入的代码。只需将路径更改为您想要的任何内容(它将在模拟器中像这样工作,要使其在 iPhone 上工作,您必须使用文档文件夹)。有一个用数组向下写入 plist 并从中读取的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多