Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 10-Apr-17 23:09pm

1 solution

Yes , you are getting null because your weatherArray has not allocate and initialize any where .
in ViewDidload() try to allocate the array like :

Objective-C
_weatherArray = [[NSMutableArray alloc]init];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900