2017-05-09 70 views
0

我有一个NSDictionary,名为“thisList”,我需要获取其关键字“list_collection”的值。无法获取NSSingleEntryDictionary的值

Xcode指示该值是“NSSingleEntryDictionary”。

该值本身包含另一个字典的数组。

请看看截图: enter image description here

现在,不管我怎么努力(objectforKey/valueforKey)或I初始化(的NSArray/NSMutableArray里/ NSDictionary中/的NSMutableDictionary)我结束了与任何类型的对象无价值。

显然,我错过了一些关于如何处理这个问题的基本知识。

我的问题:我应该如何使用键值“list_collection”初始化一个对象。

这里是JSON的(部分)转储:

Printing description of thisList: 
{ 
    archived = 0; 
    "chapter_part" = ""; 
    "chapter_title" = ""; 
    comment = ""; 
    "cover_id" = "<null>"; 
    "created_at" = "2017-01-06T12:59:04.000+01:00"; 
    "date_created" = "06 January 2017"; 
    deleted = 0; 
    id = 141384502; 
    isMyList = 1; 
    keywords =  (
    ); 
    "list_collection" =  { 
     lists =   (
         { 
       "speech_locale" = EN; 
       subject = engels; 
       words =     (
             { 
         word = attitude; 
        }, 

的我最合乎逻辑的做法是:

NSDictionary * myDic = [thisList objectForKey:@"list_collection"]; 

注:我没有明确初始化 'myDic' 在这里。

为了把事情而言,这里是我的代码:

NSString * hlists = [json objectForKey:@"lists"]; 
    NSData* data = [hlists dataUsingEncoding:NSUTF8StringEncoding]; 
    NSArray *wrtsLists = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError]; 
    Lists = [[NSMutableArray alloc]init]; 
    for (NSDictionary *thisList in wrtsLists){ 
     WordList* theList = [[WordList alloc]init]; 
     theList.title = [thisList valueForKey:@"title"]; 
     [Lists addObject:theList]; 
     NSDictionary *myDic = thisList[@"list_collection"]; 
     >>>>this is where I put a breakpoint. myDic is nil here 
    } 

感谢您的见解。

+2

你可以发布一些代码和JSON数据吗?所以我们可以帮助你解决你的问题。 – CodeChanger

+0

你声明变量的对象类型无关紧要,如果该键是正确的,它不会是“无”。类型不匹配可能(将)导致在线后面的崩溃,但它不会使值为零。 尝试使用控制台并输入“po thisList.allKeys”,看看你是否正确拼写密钥 – GreatWiz

+0

根据截图,你最*合乎逻辑的方法*应该工作。然而,我总是喜欢像'thisList [@“list_collection”]' – vadian

回答

1

你可以简单地得到“thisList”“list_collection”阵列这段代码

NSDictionary *myDic = thisList[@"list_collection"]; 
-1

使用试试这个希望它可以帮助你

NSDictionary *myDic = [thelist valueForKey:"list_collection"]; 
+0

这样的关键订阅。除非你能解释为什么明确需要KVC,否则不要使用'valueForKey'。 – vadian

0

最后,我想通了,这是一个Xcode的问题。 调试区域只是没有正确显示我的对象的值。 我重新启动Xcode,事情开始按预期工作。 我在此失去了几个小时的生活。但是,由于你的建议,我学到了很多东西。