2012-09-11 34 views
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"];時的NSDictionary或NSString的值代入的UILabel

我不知道男人入門崩潰,數據精確地打印在控制檯中,但在的UILabel(temperatureLabel)崩潰。幫助我,在此先感謝

+1

只是一個小提示,當你要給它分配一個其他的值時,不要分配和初始化'NSDictionary'。你現在只是泄漏記憶。 – rckoenes

+0

我試圖仍然崩潰... – Joker

+0

錯誤消息是[__NSCFNumber isEqualToString:]:無法識別的選擇發送到實例0x68ac960 – Joker

回答

4

如果你看看你得到它告訴你,對於關鍵temperature對象的回報是不是NSStringNSDictionaryNSNumber錯誤。

試試這個:

-(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。

+0

謝謝老兄......所以你和你的代碼很好,完美:) – Joker

0

您的詞典包含一個NSNumber實例,您正在將其分配給NSString類型的屬性。崩潰消息是你的提示。使用stringValue或其他方式將號碼的數據轉換爲字符串形式。

0

你確定你的對象是NSString類嗎?

嘗試把在這樣的:

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

如果它不會記錄到它意味着你的對象不是一個NSString,你可以嘗試像控制檯:

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

你應根據您存儲在您的詞典中的對象的種類來更改%@。

相關問題