我有 3 个类(class)。我试着像这样使用其中一个,但我知道出了点问题,但无法弄清楚我做错了什么。这是我的代码。 field 等级
@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);
}];
}
我应该怎么做才能改变这种情况?
最佳答案
您需要先实例化 Stats 和 Location 类,然后再为它们赋值。 肮脏的例子:
Venue *venue = [[Venue alloc] init];
venue.stats = [[Stats alloc] init];
venue.location = [[Location alloc] init];
关于ios - NSObject 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43572175/