シングルトン パターンと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」に渡すときに、私は立ち往生していると思います。