1

我是 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);
}
4

2 回答 2

0

尝试self.arrayOfVenues = [venues copy];

我认为当您离开街区并且该变量消失时,您可能会失去对“场地”的引用。

还:

它只在实际的“块”中填充自己,但如果我在 viewDidLoad() 中调用它来访问它,它只会打印出 null。

请记住,此块需要在您尝试访问阵列之前完成。viewDidLoad 肯定会在这个块完成之前触发,viewDidAppear 也会如此。

于 2013-07-21T16:23:51.477 回答
0

你必须分配和初始化数组。像这样

 NSArray *venues =[[NSArray alloc]init];
 venus=[result array];
于 2013-07-21T16:14:09.173 回答