ios - NSObject 返回空值

标签 ios objective-c nsobject

我有 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/

相关文章:

iphone - 如何快速调用方法但最早在下一次运行循环迭代中?

ios - 无法将正确的图像传递给 uitableviewcell

ios - 在 iphone 中显示 .key(keynote) 文件

iOS - 将 XXXViewController 缩写为 XXXVC 坏还是好?

ios - swift 2 : Strange Objective C -> Swift method's signature conversion

objective-c - NSPredicateEditorRowTemplate,用空格指定关键路径?

objective-c - 如何在 Kiwi 中 stub 方法 block ?

ios - 显示 UIPickerView 时如何向上移动 View

ios - CGRect 在 NSObject 的子类中无法识别

ios - NSCoding 意外发现 nil