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.