2017-04-23 9 views
0

私は3クラスを持っています。私はこのような人の1人を使用しようとしましたが、何かが間違っていることを知っていますが、私が間違っていることを理解できませんでした。これは私のコードです。 会場クラスNSObject Return Null

@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 

場所クラス

#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 

そして統計クラス

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

私は場所や統計情報を割り当てるしようとし、そのリターンは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); 
    }]; 

} 

この条件を変更するにはどうすればよいですか?

+1

私はこの種のアプローチを提案します。http://stackoverflow.com/questions/37409944/alternate-syntax-fo-assigning-json-data-objectforkey-to-attributes-of-class/37410905#37410905 toあなたのコードをもっと鮮明にし、@nspalvo(適切なカスタムinitメソッドの内部)のスグリを付けてください。 – Larme

+0

@Larmeありがとう私のコードを更新しました。 –

答えて

3

値を割り当てる前に、StatsクラスとLocationクラスをインスタンス化する必要があります。 汚れ例:私は、作成されるオブジェクトのプロパティを見ていない

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

、すべてのオブジェクトが初期化されていることを確認してください。