我有一个NSDictionary,名为“thisList”,我需要获取其关键字“list_collection”的值。无法获取NSSingleEntryDictionary的值
Xcode指示该值是“NSSingleEntryDictionary”。
该值本身包含另一个字典的数组。
现在,不管我怎么努力(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
}
感谢您的见解。
你可以发布一些代码和JSON数据吗?所以我们可以帮助你解决你的问题。 – CodeChanger
你声明变量的对象类型无关紧要,如果该键是正确的,它不会是“无”。类型不匹配可能(将)导致在线后面的崩溃,但它不会使值为零。 尝试使用控制台并输入“po thisList.allKeys”,看看你是否正确拼写密钥 – GreatWiz
根据截图,你最*合乎逻辑的方法*应该工作。然而,我总是喜欢像'thisList [@“list_collection”]' – vadian