3 클래스가 있습니다. 그리고 나는 이것들 중 하나를 사용하려고 시도했지만 뭔가 잘못되었다는 것을 알고 있지만 잘못하고있는 것을 알아 내지 못했습니다. 이것은 내 코드입니다. 장소 클래스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);
}];
}
이 조건을 변경하려면 어떻게해야합니까?
나는 이런 종류의 접근법을 제안합니다. http://stackoverflow.com/questions/37409944/alternate-syntax-fo-assigning-json-data-objectforkey-to-attributes-of-class/37410905#37410905 to 코드를 더 명확하게하고 @nspalvo (사용자 정의 init 메소드 내에서)를 sugestion하십시오. – Larme
@Larme 감사합니다. 코드를 업데이트했습니다. –