Search code examples
iosobjective-cnsobject

NSObject Returning Null


I have 3 class. and I tried to use one of them like this but I know something is wrong but couldn't figured out what I'm doing wrong. This is my code. Venue Class

@interface Venue : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *venueId;
@property Location *location;
@property Stats *stats;

+(Venue*) sharedInstance;
-(void)getVenues:(NSString*)ll :(NSString*)query completionHandler:(void (^)(NSMutableArray *array))completionHandler;

@end

Location Class

#import <Foundation/Foundation.h>

@interface Location : NSObject

@property (nonatomic, strong) NSString *address;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *lat;
@property (nonatomic, strong) NSString *lng;
@property (nonatomic, strong) NSString *crossStreet;
@property NSNumber *distance;
@property (nonatomic, strong) NSString *cc;
@property (nonatomic, strong) NSString *country;

@end

And Stats Class

@interface Stats : NSObject
@property NSNumber *usersCount;
@property NSNumber *checkinsCount;
@property NSNumber *tipCount;
@end

When I trying to assign location and stats values its returns null.

-(void)getVenues:(NSString*)ll :(NSString*)query completionHandler:(void (^)(NSMutableArray *array))completionHandler {
    NSDate *date = [[NSDate alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyymmdd"];
    NSString *today = [dateFormatter stringFromDate:date];
    NSMutableArray *response = [[NSMutableArray alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/venues/search?v=%@&ll=%@&query=%@&client_id=%@&client_secret=%@&intent=checkin", kBaseUrl,today,ll,query,kClientId,kClientSecret];
    NSLog(@"%@", url);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *objects = (NSDictionary *)responseObject;
        NSDictionary *venues = objects[@"response"][@"venues"];
        for (NSDictionary *object in venues) {
            Venue *venue = [[Venue alloc] init];
            venue.name = object[@"name"];
            venue.venueId = object[@"id"];
            venue.stats.usersCount = object[@"stats"][@"usersCount"];
            venue.stats.checkinsCount = object[@"stats"][@"checkinsCount"];
            venue.stats.tipCount = object[@"stats"][@"tipCount"];
            venue.location.address = object[@"location"][@"address"];
            venue.location.city = object[@"location"][@"city"];
            venue.location.lat = object[@"location"][@"lat"];
            venue.location.lng = object[@"location"][@"lng"];
            venue.location.crossStreet = object[@"location"][@"crossStreet"];
            venue.location.distance = object[@"location"][@"distance"];
            venue.location.cc = object[@"location"][@"cc"];
            venue.location.country = object[@"location"][@"country"];
            NSLog(@"%@",venue.location);
            [response addObject:venue];
        }
        completionHandler(response);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error: %@", error);
    }];

}

What should I do for change this condition ?


Solution

  • You need to instantiate Stats and Location classes before assign values to them. Dirty example:

    Venue *venue = [[Venue alloc] init];
    venue.stats = [[Stats alloc] init];
    venue.location = [[Location alloc] init];