0
-(void) httpDataDidFinishLoadingWithData:(NSData *)theData
{
    m_activityLoaded=NO;
    temp=[[NSString alloc] initWithData:[dataLoader httpData]
                               encoding:NSUTF8StringEncoding];
    NSLog(@"TEMP IS TEMP %@", temp);

    NSDictionary *dict = [[NSDictionary alloc]init];
    dict = [[temp JSONValue] objectForKey:@"location"];

    NSDictionary *dict1 = [[NSDictionary alloc]init];
    dict1 = [[temp JSONValue] objectForKey:@"wind"];


    NSDictionary *dict2 = [[NSDictionary alloc]init];
    dict2 = [dict1 objectForKey:@"direction"];

    NSDictionary *dict3 = [[NSDictionary alloc]init];
    dict3 = [[temp JSONValue] objectForKey:@"atmosphere"];

    NSDictionary *dict4 = [[NSDictionary alloc]init];
    dict4 = [[temp JSONValue] objectForKey:@"condition"];
    NSDictionary *dict5 = [[NSDictionary alloc]init];
    dict5 = [dict4 objectForKey:@"text"];
    NSDictionary *dict6 = [[NSDictionary alloc]init];
    dict6 = [dict4 objectForKey:@"code"];
    NSDictionary *dict7 = [[NSDictionary alloc]init];
    dict7 = [dict4 objectForKey:@"temperature"];

    temperatureLabel.text = [dict4 objectForKey:@"temperature"];
}

崩溃发生在temperatureLabel.text = [dict4 objectForKey:@"temperature"];

我不知道,数据完全打印在控制台中,但在 UILABEL(温度标签)处崩溃。帮帮我,提前谢谢

4

3 回答 3

4

如果您查看收到的错误,它会告诉您键的对象返回temperature不是 aNSStringNSDictionary而是 a NSNumber

试试这个:

-(void) httpDataDidFinishLoadingWithData:(NSData *)theData {
    m_activityLoaded=NO;
    temp=[[NSString alloc] initWithData:[dataLoader httpData]
                               encoding:NSUTF8StringEncoding];
    NSLog(@"TEMP IS TEMP %@", temp);

    NSDictionary *dict = [[temp JSONValue] objectForKey:@"location"];
    NSDictionary *dict1 =  [[temp JSONValue] objectForKey:@"wind"];
    NSDictionary *dict2 = [dict1 objectForKey:@"direction"];

    NSDictionary *dict3 = [[temp JSONValue] objectForKey:@"atmosphere"];
    NSDictionary *dict4 = [[temp JSONValue] objectForKey:@"condition"];
    NSDictionary *dict5 = [dict4 objectForKey:@"text"];
    NSDictionary *dict6 =  [dict4 objectForKey:@"code"];
    NSNumber *temperature = [dict4 objectForKey:@"temperature"];

    temperatureLabel.text = [NSString stringWithFormat:@"%@", temperature]; 
}

您可能想要查看NSNumberFormatter格式化程序的温度,例如:°F 或°C。

于 2012-09-11T09:31:31.660 回答
0

您的字典包含一个NSNumber实例,并且您将其分配给 type 的属性NSString。崩溃消息是您的提示。使用stringValue或其他方式将数字的数据转换为字符串形式。

于 2012-09-11T09:32:02.020 回答
0

你确定你的对象是 NSString 类的吗?

尝试放入类似的东西:

if([[dict4 objectForKey:@"temperature"] isKindOfClass:[NSString class]])
   NSLog(@"lalala");

如果它没有被记录到控制台,则意味着您的对象不是 NSString,您可以尝试以下操作:

temperatureLabel.text = [NSString stringWithFormat:@"%@", [dict4 objectForKey:@"temperature"]];

您应该根据存储在字典中的对象类型更改 %@。

于 2012-09-11T09:39:39.393 回答