我是 RestKit 的初学者(我有一些 iOS 经验),我的 NSArray 没有被 RKMappingResult 填充时遇到了一些问题。它只在实际的“块”中填充自己,但如果我在 viewDidLoad() 中调用它来访问它,它只会打印出 null。
有人可以建议或提供文章的任何提示/链接吗?
非常感谢!
这是我的代码 -
-(void) loadData
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Venue class]];
[mapping addAttributeMappingsFromDictionary:@{
@"name": @"name", @"location.address" : @"address", @"location.city" : @"city"}];
NSString *urlString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=55.903324,-3.171383&categoryId=4bf58dd8d48988d18f941735&client_id=%s&client_secret=%s&v=20130717", kCLIENTID, kCLIENTSECRET];
NSURL *baseUrl = [NSURL URLWithString:urlString];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"response.venues" statusCodes:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:baseUrl];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
//set the array of venues to be the result of what we got back
//NSLog(@"%@", [result array]);
NSArray *venues = [result array];
self.arrayOfVenues = venues; //self.arrayOfVenues is always null when I call it in the other methods
} failure:nil];
[operation start];
}
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
NSLog(@"%@", self.arrayOfVenues);
}
- (void)viewDidLoad
{
[super viewDidLoad];
//load the venue data
self.title = @"Venues";
[self loadData];
NSLog(@"%@", self.arrayOfVenues);
}