Iphone 从完成块内部修改实例变量
我已经编写了一些代码来从completionblock处理程序内部修改实例变量,该处理程序是“reverseGeocodeLocation”方法的一部分。以下是代码:Iphone 从完成块内部修改实例变量,iphone,objective-c,block,instance-variables,Iphone,Objective C,Block,Instance Variables,我已经编写了一些代码来从completionblock处理程序内部修改实例变量,该处理程序是“reverseGeocodeLocation”方法的一部分。以下是代码: - (void)reverseGeocodeLocation:(CLLocation *)aLocation { CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; __block __strong NSArray *dataArray; [geoCoder
- (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];
}
嗯。。。如您所见,我想填充实例变量“tableViewRows”。临时数组“dataArray”的填充工作得非常好!将显示所有值。方法中实例变量的填充也很好!但只有在完成区块内
一旦我尝试访问完成块之外的实例变量,我就会收到一条错误消息,即数组中没有值。我错过了什么重要的事情吗
我已经读到,“reverseGeocodeLocation”是一条异步消息,当我尝试读取实例变量时,它还不能完成。但我认为不应该是这样,因为已经调用了完成块,我可以在完成块中记录数组的条目
我现在不知道该怎么办。我有一种感觉,这是一个非常简单的解决方案,我现在几个小时都不明白。也许你能帮我一把
谢谢所以看起来您是在告诉table view中始终有5行,但这可能不是真的。您应该询问self.tableViewRows.count以了解实际计数。有鉴于此,我认为您在第一次加载表视图时崩溃了,当时它试图重新加载自己,但tableViewRows是空的
另外,您应该将tableView分配给viewDidLoad中的属性什么是“tableViewRows”的定义?它结实/结实吗?如果没有,它会在你尝试使用它之前发布。嗨!杰普。。。它是@property(非原子,强)NSMutableArray*tableViewRows;确保在设置数据和调用表视图之前切换回主线程。或者至少使属性原子化并在主线程上调用表。您到底得到了什么异常以及在哪里?(显示代码)当我尝试显示“cellforRowAtIndexPath”中的内容时:cell.textLabel.text=[self.tableViewRows objectAtIndex:indexPath.row];我收到消息:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限”我也尝试过。。但当我这么做时,整个应用程序都是免费的。而且根本没有显示任何表格。但除此之外,该应用程序不再响应。数据似乎都在那里。但只有在完成区块内。它无法将代码复制到实例变量中并保持不变。我不认为这是无响应的,请将表的init行更改为
[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]
,这样您的帧是正确的,而表是普通的,这样您就可以看到滚动工作了,只是尝试了一下。。那部分你说得对。。该应用程序响应迅速。但我刚刚发现,如果我尝试在reverseGeocodeLocation方法中记录“dataArray”变量,并在完成块的右括号后放置一个NSLOG,那么该数组中就没有任何内容。即使它被指定为_块变量。因此,内容仅在完成块内部可见。我觉得这很奇怪。dataArray变量应该由完成块填充数据,不是吗?这就是为什么我的实例var也是空的!?嗯,不,当您在块外记录它时,它是空的,因为块尚未启动,请记住,它的主体将在您退出reverseGeocodeLocation
Hm后执行。。。但这让我觉得更奇怪。。。我可以看到块内数据数组的日志输出。街区应该在那之后开火,不是吗?在这么多层面上看到这一点真是太糟糕了:)我觉得我看到了问题就在眼前,但我不知道该怎么办