我正在做作业以使用单例模式和ASIHTTPRequest
.
JSON 格式的 URL 中的数据如下所示:
{ "data":
{ "current_condition":
[ {
"cloudcover": "51",
"humidity": "66",
"observation_time": "12:44 PM",
"precipMM": "0.0",
"pressure": "1002",
"temp_C": "30",
"temp_F": "86",
"visibility": "10",
"weatherCode": "116",
"weatherDesc": [ {"value": "Partly Cloudy" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png" } ],
"winddir16Point": "S",
"winddirDegree": "170",
"windspeedKmph": "19",
"windspeedMiles": "12" } ],
"request": [ {
"query": "Lat 22.49 and Lon 114.14",
"type": "LatLon" } ],
"weather": [ {
"date": "2012-06-06",
"precipMM": "0.0",
"tempMaxC": "30",
"tempMaxF": "86",
"tempMinC": "26",
"tempMinF": "79",
"weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
"winddir16Point": "SE",
"winddirDegree": "136",
"winddirection": "SE",
"windspeedKmph": "17",
"windspeedMiles": "11"
},
{
"date": "2012-06-07",
"precipMM": "0.1",
"tempMaxC": "30",
"tempMaxF": "87",
"tempMinC": "27",
"tempMinF": "80",
"weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
"winddir16Point": "ESE",
"winddirDegree": "121",
"winddirection": "ESE",
"windspeedKmph": "15",
"windspeedMiles": "10"
},
{
"date": "2012-06-08",
"precipMM": "2.1",
"tempMaxC": "31",
"tempMaxF": "87",
"tempMinC": "27",
"tempMinF": "81",
"weatherCode": "116",
"weatherDesc": [ {"value": "Partly Cloudy" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ],
"winddir16Point": "SSE",
"winddirDegree": "166",
"winddirection": "SSE",
"windspeedKmph": "17",
"windspeedMiles": "11"
},
{
"date": "2012-06-09",
"precipMM": "2.8",
"tempMaxC": "32",
"tempMaxF": "89",
"tempMinC": "28",
"tempMinF": "82",
"weatherCode": "176",
"weatherDesc": [ {"value": "Patchy rain nearby" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ],
"winddir16Point": "SSW",
"winddirDegree": "198",
"winddirection": "SSW",
"windspeedKmph": "17",
"windspeedMiles": "11"
},
{
"date": "2012-06-10",
"precipMM": "13.0",
"tempMaxC": "32",
"tempMaxF": "90",
"tempMinC": "28",
"tempMinF": "82",
"weatherCode": "116",
"weatherDesc": [ {"value": "Partly Cloudy" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ],
"winddir16Point": "SW",
"winddirDegree": "220",
"winddirection": "SW",
"windspeedKmph": "22",
"windspeedMiles": "14"
} ]
}
}
在我的 AppData.m 中,代码如下所示:
- (void)requestFinished:(ASIHTTPRequest *)request {
NSData* responseData = [request responseData];
NSDictionary* resultDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:NULL];
NSDictionary* dataDict = [resultDict objectForKey:@"data"];
NSArray* myArray = [dataDict objectForKey:@"weather"];
if(weatherDataArray == nil)
weatherDataArray = [[NSMutableArray alloc] init];
[weatherDataArray setArray:myArray];
}
在 myWeather.m 中,代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
myWeatherDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myWeatherDataCell"];
// get the view controller's info dictionary based on the indexPath's row
NSDictionary* item = [[AppData sharedData].weatherDataArray objectAtIndex:indexPath.row];
cell.maxTempLabel.text = [item objectForKey:@"tempMaxC"];
cell.minTempLabel.text = [item objectForKey:@"tempMinC"];
cell.dateLabel.text = [item objectForKey:@"date"];
cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
NSArray* weatherIconUrl = [item objectForKey:@"weatherIconUrl"];
NSDictionary* value = [weatherIconUrl valueForKey:@"value"];
NSString* urlString = [NSString stringWithFormat:@"%@",value];
NSData* url = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
cell.iconView.image = [UIImage imageWithData:url];
NSLog(@"weatherIconUrl" "%@",urlString);
return cell;
}
表格视图可以显示
cell.maxTempLabel.text = [item objectForKey:@"tempMaxC"];
cell.minTempLabel.text = [item objectForKey:@"tempMinC"];
cell.dateLabel.text = [item objectForKey:@"date"];
除了 iconview.image。
我尝试使用 NSlog
NSString* urlString = [NSString stringWithFormat:@"%@",value];
它可以显示如下:
2012-11-11 11:51:46.100 MyWeather[1583:1a603] weatherIconUrl(
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.101 MyWeather[1583:1a603] weatherIconUrl(
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
)
2012-11-11 11:51:46.102 MyWeather[1583:1a603] weatherIconUrl(
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.102 MyWeather[1583:1a603] weatherIconUrl(
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.103 MyWeather[1583:1a603] weatherIconUrl(
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
)
但是当我尝试 NSLog NSData* url 时,结果是(null)。
所以我认为当“NSString * urlString”将数据传递给“NSData * url”时我被困住了。