如何从nsobject类传递nsarray来查看控制器类? [英] How to pass nsarray from an nsobject class to view controller class?
问题描述
我是客观的新手。我正在尝试创建一个天气应用程序。我从开放的天气图解析数据。我已将解析后的数据存储到数组中。现在想要从其他类访问数组值,但获取空值。
有谁可以帮助我?在此先感谢。
我尝试过:
这里是我的nsobject类我存储数据并尝试将其发送到viewcontroller。
- (void)getCurrentWeather:(NSString *)查询
{
NSString * const BASE_URL_STRING = @http://api.openweathermap.org/data/2.5/weather?q=;
NSString * const API_KEY = @& APPID = 35e37cecbd835f3949f6d67cb25b0706;
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(fetchedData :) withObject:data waitUntilDone:YES];
});
}
- (无效)fetchedData:(NSData *)responseData {
NSError *错误;
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions错误:& 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] objectAtIndex:0] objectForKey:@description];
weatherArray = [[NSMutableArray alloc] initWithObjects:cit yName,weatherDescription,
[NSString stringWithFormat:@%d,currentTempCelsius],
[NSString stringWithFormat:@%d,maxTemp],
[NSString stringWithFormat:@%d,minTemp],nil];
我有NSObject.h文件as-
@interface WeatherData:NSObject
@property( nonatomic)NSString * weatherDescription;
@property(强,非原子)NSString * currentTemp;
@property(nonatomic)int maxTempCelsius;
@ property(nonatomic)int minTempCelsius;
@property(nonatomic,retain)NSMutableArray * weatherArray;
- (void)getCurrentWeather:(NSString *)查询;
@end
在我的视图控制器中 -
.h文件:
@property(非原子,保留)NSMutableArray * weatherResultArray;
.m档案:
- (无效)searchButtonClicked:(UIButton *)发件人
{
[self.view endEditing:YES];
WeatherData * weather = [[WeatherData alloc] init];
[weather getCurrentWeather:_textField.text];
self.weatherResultArray = weather.weatherArray;
//temperatureLabel.text = [NSString stringWithFormat:@%d°,weather.currentTempCelsius];
}
我只想在UILable中显示结果。
是的,你得到null因为你的weatherArray没有分配和初始化任何地方。
ViewDidload()中的
尝试分配如下数组:
_ weatherArray = [[NSMutableArray alloc] INIT];
I am new to objective c. I am trying to create a weather app. where i parsed data from open weather map. I have stored the parsed data to an array. now want to access the array value from other class but getting null value.
Can anyone help me? Thanks in advance.
What I have tried:
here is my nsobject class where i am storing data and trying to send that to viewcontroller.
- (void)getCurrentWeather:(NSString *)query
{
NSString *const BASE_URL_STRING = @"http://api.openweathermap.org/data/2.5/weather?q=";
NSString *const API_KEY = @"&APPID=35e37cecbd835f3949f6d67cb25b0706";
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(fetchedData:)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"] objectAtIndex:0] objectForKey:@"description"];
weatherArray = [[NSMutableArray alloc] initWithObjects:cityName, weatherDescription,
[NSString stringWithFormat:@"%d", currentTempCelsius],
[NSString stringWithFormat:@"%d", maxTemp],
[NSString stringWithFormat:@"%d", minTemp],nil];
I have NSObject.h file as-
@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
In my view controller -
.h file:
@property (nonatomic, retain) NSMutableArray *weatherResultArray;
.m file:
-(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];
}
I just want to show the results in UILable.
Yes , you are getting null because your weatherArray has not allocate and initialize any where .
in ViewDidload() try to allocate the array like :
_weatherArray = [[NSMutableArray alloc]init];
这篇关于如何从nsobject类传递nsarray来查看控制器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!