Objective-C foreach用于创建不起作用的NSDictionary

时间:2014-03-17 13:38:07

标签: ios objective-c json nsdictionary

我正在尝试使用以下代码对NSDictionary进行排序:

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&err];
for (NSDictionary* lesson in jsonDict[@"data"][@"lessons"]) {
    data[lesson[@"day"]][lesson[@"index"]][@"title"] = lesson[@"title"];
}

如果我记录lesson[@"day"]等的值,则值似乎是正确的。不知何故,如果我在循环后记录data[@"1"][@"1"]它只是空的。所以问题似乎是将值设置为NSDictionary数据。

原始JSON字符串包含包含所有课程的列表。要正确访问它们,我想要这种格式的新NSDictionary:data [day] [index] [field]

JSON代码如下所示:

{"status":"success","data":{"lessons":[{"id":1,"index":"1","day":"1","title":"Mathe","teacher":"Mr. xyz","room":"103","ind":"1"},{"id":2,"index":"1","day":"2","title":"Physik","teacher":"Mr xyz","room":"PH4","ind":"1"},...

2 个答案:

答案 0 :(得分:2)

问题是(我假设)

data[lesson[@"day"]][lesson[@"index"]][@"title"] = lesson[@"title"];

不会自动创建中间词典

data[lesson[@"day"]]
data[lesson[@"day"]][lesson[@"index"]]

您必须首先检查它们是否存在,并指定一个可变字典(如果它们尚未存在) 存在。 (与Perl哈希一样没有Autovivification!)类似于:

if (data[lesson[@"day"]] == nil) {
    data[lesson[@"day"]] = [NSMutableDictionary dictionary];
}
if (data[lesson[@"day"]][lesson[@"index"]] == nil) {
    data[lesson[@"day"]][lesson[@"index"]] = [NSMutableDictionary dictionary];
}
data[lesson[@"day"]][lesson[@"index"]][@"title"] = lesson[@"title"];

完整的工作代码:

NSString *str = @"{\"status\":\"success\",\"data\":{\"lessons\":[{\"id\":1,\"index\":\"1\",\"day\":\"1\",\"title\":\"Mathe\",\"teacher\":\"Mr. xyz\",\"room\":\"103\",\"ind\":\"1\"},{\"id\":2,\"index\":\"1\",\"day\":\"2\",\"title\":\"Physik\",\"teacher\":\"Mr xyz\",\"room\":\"PH4\",\"ind\":\"1\"}]}}";
NSError *err;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&err];
NSMutableDictionary *data = [NSMutableDictionary dictionary];

for (NSDictionary* lesson in jsonDict[@"data"][@"lessons"]) {
    if (data[lesson[@"day"]] == nil) {
        data[lesson[@"day"]] = [NSMutableDictionary dictionary];
    }
    if (data[lesson[@"day"]][lesson[@"index"]] == nil) {
        data[lesson[@"day"]][lesson[@"index"]] = [NSMutableDictionary dictionary];
    }
    data[lesson[@"day"]][lesson[@"index"]][@"title"] = lesson[@"title"];
}
NSLog(@"%@", data);

输出:

{
    1 =     {
        1 =         {
            title = Mathe;
        };
    };
    2 =     {
        1 =         {
            title = Physik;
        };
    };
}

答案 1 :(得分:1)

NSJSONSerialization创建字典和数组,其中包含其他字典,数组和NSString,NSNumber或NSNull类型的值。

您应该重新思考编写极其复​​杂的代码行的策略,但要逐一提取内容,以减少混乱,并使调试成为可能。像

NSDictionary* data = jsonDict [@"data"];
NSArray* lessons = data [@"lessons"];

for (NSDictionary* lesson in lessons)
{
    NSString* lessonDay = lesson [@"day"];
    ...
}

等等(如果你不反复一次又一次地查找相同的东西,它也会使你的代码运行得更快)。

现在你的bug在哪里:dict [@" key"] = data;调用setObject:forKey :.然而,dict [@" key1"] [@" key2"]调用objectForKey:@" key1"在字典上,这将给你一个nil值,然后setObjectForKey:将使用nil接收器调用。