从完成块内部修改实例变量



我写了一些代码来修改一个实例变量从内部的completionblock处理程序是"reverseGeocodeLocation"方法的一部分。下面是代码:

- (void)reverseGeocodeLocation:(CLLocation *)aLocation {
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    __block __strong NSArray *dataArray;
    [geoCoder reverseGeocodeLocation:aLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks lastObject];
            CLLocation *userLocation = [placemark location];
            NSString *latitude = [NSString stringWithFormat:@"Lat. %f degrees", userLocation.coordinate.latitude];
            NSString *longitude = [NSString stringWithFormat:@"Long. %f degrees", userLocation.coordinate.longitude];
            NSString *altitude = [NSString stringWithFormat:@"Alt. %f m", userLocation.altitude];
            NSString *speed = [NSString stringWithFormat:@"Speed %f m/s", userLocation.speed];
            NSString *course = [NSString stringWithFormat:@"Course %f degrees", userLocation.course];
            dataArray = @[latitude, longitude, altitude, speed, course];
        // The data array is perfectly populated!!
            NSLog(@"Just checking... %@", dataArray);
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.tableViewRows = [NSMutableArray arrayWithArray:dataArray];
            [self.tableView reloadData];
        });
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *currentLocation = [locations lastObject];
    [self reverseGeocodeLocation:currentLocation];
}

这里是tableView的代码:

#pragma mark -
#pragma mark TabeView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = [self.tableViewRows objectAtIndex:indexPath.row];
    }
    return cell;
}

和VC的其余部分:

- (id)init {
    self = [super init];
    if (self) {
        if ([CLLocationManager locationServicesEnabled]) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self; // location manager delegate
            self.locationManager.distanceFilter = 1000;
            [self.locationManager startUpdatingLocation];
        }
        // Create the Rows Array for the TableView
        self.tableViewRows = [[NSMutableArray alloc] init];
    }
    return self;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    // Create the TableView
    UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview:table];
}

嗯…正如你所看到的,我想填充实例变量"tableviewws"。临时数组"dataArray"的填充工作得非常好!所有的值都显示出来了。方法内实例变量的填充也工作得很好!但只能在完成块内。

当我试图访问完成块之外的实例变量时,我收到一条错误消息,数组中没有值。我是不是漏掉了什么重要的东西?

我已经读到"reverseGeocodeLocation"是一个异步消息,无法完成,当我试图读取实例var.但在我看来,不应该是这样,因为完成块已经被调用,我可以记录完成块内部数组的条目。

我不知道现在该做什么。我有一种感觉,这是一个非常简单的解决方案,但我现在几个小时都不明白。也许你能帮帮我?

谢谢

看起来你在告诉表视图你的总是有5行,但这可能不是真的。您应该向self.tableViewRows.count询问实际计数。鉴于此,我认为你在第一次加载表视图时崩溃了,当它试图重新加载自己时,但tableviewws是空的。

另外,你应该将tableView分配给viewDidLoad

中的属性

最新更新