2010-05-12 79 views
0

用于获取plist类型为dictionary的项目的数据类型,即nsmutabledictionary或nsdictionary是什么?因为我使用以下代码从plist中的字典数组中检索字典对象。从plist中获取字典对象的问题iphone sdk

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中,它们是字典对象。那为什么这个错误呢?

编辑:

基本上,我在控制台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; 
} 
) 

我想要从内容数组这些字典对象。

任何人都可以请帮忙吗?

这真的很紧急。

Thanx提前。

+0

有什么确切的内容你.plist(不要总结它)?什么是确切的错误信息?如果错误导致你的应用程序崩溃,在哪一行崩溃? – 2010-05-12 09:46:51

+0

plist内容如上。我得到objc_msgsend消息,在控制台中没有任何东西。应用程序在上述行崩溃。 – neha 2010-05-12 10:02:28

+0

那里有什么“故事”?什么是“根”标签? PList具有相当严格的语法。数组是“数组”,字典使用“字典”标签等。 – bealex 2010-05-12 10:09:38

回答

2

NSDictionary。你不能简单地说

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; 

并希望,这是一个可变的字典现在。这仍然是一种不可改变的正常情节。所以,你应该写这样的东西:

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

Alexander Babaev,我试着按照你的建议做,但没有成功,它仍然在同一行显示我objc_msgsend。我想从nsarray中获取第一个字典对象。 – neha 2010-05-12 08:40:10

+0

你能显示确切的消息,它显示你吗?什么信息是错误的,它发送了什么对象? – bealex 2010-05-12 10:06:52

+0

它显示我objec_msgsend。控制台没有什么在这里我创建了一个nsdictionaries数组。这可以吗?或者它应该是一个数组字典? – neha 2010-05-12 10:09:36