将字典分配给变量

时间:2013-08-26 12:25:06

标签: ios objective-c nsdictionary

当检查self.weatherData时,即使“data”中有数据,我什么也得不到。这是我的功能:

- (void)handleNetworkResponse:(NSData *)myData
{
    //NSMutableDictionary *data = [NSMutableDictionary dictionary];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] init];

    // now we'll parse our data using NSJSONSerialization
    id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];

    // typecast an array and list its contents
    NSDictionary *jsonArray = (NSDictionary *)myJSON;

    // take a look at all elements in the array
    for (id element in jsonArray) {

        id key = [element description];

        id innerArr = [jsonArray objectForKey:key];

        NSDictionary *inner = (NSDictionary *)innerArr;

        if ([inner conformsToProtocol:@protocol(NSFastEnumeration)]) {

            for(id ele in inner) {

                id innerKey = [ele description];
                [data setObject:[[inner valueForKey:innerKey] description] forKey:[ele description]];
            }
        }
        else {

            [data setObject:[inner description] forKey:[element description]];
        }
    }

    NSLog([data description]);
    self.weatherData = data;
}

然而,当检查self.weatherData时,即使“data”中有数据,我也什么都没有。

1 个答案:

答案 0 :(得分:0)

当我从异步方法将变量分配给变量时,问题是数据不存在:D 现在通过添加委托回叫

来解决所有问题