我是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);
}
答案 0 :(得分:0)
你必须分配& init数组。像这样
NSArray *venues =[[NSArray alloc]init];
venus=[result array];
答案 1 :(得分:0)
尝试self.arrayOfVenues = [venues copy];
我认为当你离开街区时,你可能会失去对“场地”的引用,而且这个变量就会消失。
另外:
它只会填充实际的“阻止”,但如果我打电话访问 它在viewDidLoad()中,只打印出null。
请记住,在尝试访问阵列之前,需要完成此块。 viewDidLoad肯定会在此块完成之前触发,viewDidAppear也是如此。