我是Objective-C的新手。我正在尝试创建一个天气应用程序,我从开放的天气图解析数据。我已将解析后的数据存储到数组中。现在想要从其他类访问数组值但是获取空值。
任何人都可以帮助我吗?
我尝试过:
这是我的NSObject
类,我在那里存储数据并尝试将其发送给视图控制器:
- (void)getCurrentWeather:(NSString *)query
{
NSString *const BASE_URL_STRING = @"http://api.openweathermap.org/data/2.5/weather?q=";
NSString *const API_KEY = @"&APPID=APIKEYSTRING";
NSString *weatherURLText = [NSString stringWithFormat:@"%@%@%@",
BASE_URL_STRING, query,API_KEY];
NSURL *weatherURL = [NSURL URLWithString:weatherURLText];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:weatherURL];
[self performSelectorOnMainThread:@selector(fetchedDataSmile | :) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString* cityName = [json objectForKey:@"name"];
int currentTempCelsius = (int)[[[json objectForKey:@"main"] objectForKey:@"temp"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int maxTemp = (int)[[[json objectForKey:@"main"] objectForKey:@"temp_max"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int minTemp = (int)[[[json objectForKey:@"main"] objectForKey:@"temp_min"] intValue] - ZERO_CELSIUS_IN_KELVIN;
NSString *weatherDescription = [[[json objectForKey:@"weather"] objectAtIndexBlush | :O ] objectForKey:@"description"];
weatherArray = [[NSMutableArray alloc] initWithObjects:cityName, weatherDescription,
[NSString stringWithFormat:@"%d", currentTempCelsius],
[NSString stringWithFormat:@"%d", maxTemp],
[NSString stringWithFormat:@"%d", minTemp],nil];
我有NSObject.h文件:
@interface WeatherData : NSObject
@property (nonatomic) NSString *weatherDescription;
@property (strong, nonatomic) NSString *currentTemp;
@property (nonatomic) int maxTempCelsius;
@property (nonatomic) int minTempCelsius;
@property (nonatomic, retain) NSMutableArray *weatherArray;
- (void)getCurrentWeather:(NSString *)query;
@end
在我的视图控制器中:
.h文件:
@property (nonatomic, retain) NSMutableArray *weatherResultArray;
.m文件:
-(void)searchButtonClicked:(UIButton*)sender
{
[self.view endEditing:YES];
WeatherData *weather = [[WeatherData alloc] init];
[weather getCurrentWeather:_textField.text];
self.weatherResultArray = weather.weatherArray;
//temperatureLabel.text = [NSString stringWithFormat:@"%d°",weather.currentTempCelsius];
}
我只想在UILabel中显示结果。
答案 0 :(得分:0)
您是否尝试在此方法中返回NSMutable数组
- (NSMutableArray*)getCurrentWeather:(NSString *)query
而不是这个,
- (void)getCurrentWeather:(NSString *)query
这是验证的最简单方法,并且可以在单个语句中检索值:
self.weatherResultArray = [weather getCurrentWeather:_textField.text];
还有一点,不要忘记将weatherResultArray分配并初始化为:
self.weatherResultArray = [[NSMutableArray alloc]init];
答案 1 :(得分:0)
在NSObject类中,定义天气协议。
//NSObject.h file
@protocol WeatherDelegate<NSObject>
-(void)getWeatherData:(YourNSObjectClass*)viewController getWeatherData:(NSMutableArray*)array;
@end
// NSObject.m文件,在
中- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString* cityName = [json objectForKey:@"name"];
int currentTempCelsius = (int)[[[json objectForKey:@"main"] objectForKey:@"temp"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int maxTemp = (int)[[[json objectForKey:@"main"] objectForKey:@"temp_max"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int minTemp = (int)[[[json objectForKey:@"main"] objectForKey:@"temp_min"] intValue] - ZERO_CELSIUS_IN_KELVIN;
NSString *weatherDescription = [[[json objectForKey:@"weather"] objectAtIndexBlush | :O ] objectForKey:@"description"];
weatherArray = [[NSMutableArray alloc] initWithObjects:cityName, weatherDescription,
[NSString stringWithFormat:@"%d", currentTempCelsius],
[NSString stringWithFormat:@"%d", maxTemp],
[NSString stringWithFormat:@"%d", minTemp],nil];
id<WeatherDelegate> strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:@selector(getWeatherData:getWeatherData:)])
{
[strongDelegate getWeatherData:self getWeatherData:weatherArray];
}
}
在yourViewController类中,添加此WeatherData协议并在.m文件中添加委托函数以获取数据。
@interface yourViewControllerClass()<WeatherDelegate>
{
YourNSObjectClass *nsClass;
NSMutableArray *dataArray;
}
-(void)getWeatherData:(YourNSObjectClass*)viewController getWeatherData:(NSMutableArray*)array{
dataArray = [[NSMutableArray alloc]initWithArray:array];
}
-(void)searchButtonClicked:(UIButton*)sender
{
[self.view endEditing:YES];
WeatherData *weather = [[WeatherData alloc] init];
[weather getCurrentWeather:_textField.text];
self.weatherResultArray = dataArray;
//temperatureLabel.text = [NSString stringWithFormat:@"%d°",weather.currentTempCelsius];
}