简体   繁体   English

从 plist 获取数组中的字典对象的问题

[英]Problem with fetching dictionary objects in array from plist

What is the datatype you use to fetch items whose type is dictionary in plist ie nsmutabledictionary or nsdictionary?您用来获取 plist 中类型为字典的项目的数据类型是什么,即 nsmutabledictionary 或 nsdictionary? Because I'm using following code to retrieve dictionary objects from an array of dictionaries in plist.因为我使用以下代码从 plist 中的字典数组中检索字典对象。

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

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

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

Here, on first line it's showing me objc_msgsend.在这里,第一行显示的是 objc_msgsend。 ContentArray is an nsarray and it's contents are showing 2 objects that are there in plist. ContentArray 是一个 nsarray,它的内容显示了 plist 中的 2 个对象。 In plist they are dictionary objects.在 plist 中,它们是字典对象。 Then why this error?那为什么会出现这个错误呢?

Edit编辑

Basically, the contents of my contentArray in console are as shown below :基本上,我的 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;
}
)

I want to retrieve these dictionary objects from content array.我想从内容数组中检索这些字典对象。

NSDictionary. NSD词典。 You can't simply say你不能简单地说

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; 

and hope, that it's a mutable dictionary now.并希望它现在是一个可变的字典。 It's still a normal immutable distionary.它仍然是一个正常的不可变的 distionary。 So, you should write something like:所以,你应该写一些类似的东西:

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

That'll create mutable dictionary from one that is in the plist.这将从 plist 中的一个创建可变字典。

You can read about it in the "Property List Programming Guide", http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/index.html您可以在“属性列表编程指南”中阅读它, http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/index.html

Update:更新:

Also you have a strange plist contents.你还有一个奇怪的 plist 内容。 Available xml-plist types are mentioned here: 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/AboutPropertyLists/AboutPropertyLists.html#//apple_ref/doc/uid/10000048i-CH3-SW1

And overall xml-plist structure is described here: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html#//apple_ref/doc/uid/10000048i-CH6-SW1此处描述了整体 xml-plist 结构: http : //developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html#//apple_ref/doc/uid/10000048i-CH6-开关1

Working piece of code工作代码段

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM