将CLLocationCoordinates2D转换为NSValue

我只是试图将CLLocationCoordinates转换为NSValue ,以便在我的应用程序中稍后带回的数组中使用。 这是我的代码:

  NSUInteger count = [self.locations count]; CLLocationCoordinate2D coordinates[count]; for (NSInteger i = 0; i < count; i++) { coordinates[i] = [(CLLocation *)self.locations[i] coordinate]; NSValue *locationValue = [NSValue valueWithMKCoordinate:coordinates[i]]; [_locationsArray addObject:locationValue]; NSLog(@"location = %@", _locationsArray); } 

但是, NSLog显示数组中填充了(Null)值。 我不确定我在哪里出错,有人指出我正确的方向?

我没有看到coordinates数组中有任何需要。 你可能没有初始化它。 尝试:

 _locationsArray = [NSMutableArray array]; for (CLLocation *location in self.locations) { NSValue *locationValue = [NSValue valueWithMKCoordinate: location.coordinate]; [_locationsArray addObject:locationValue]; NSLog(@"location = %@", _locationsArray); } 

您可以通过以下操作将CLLocationCoordinates2D转换为NSValue,但是您是否检查过坐标是否包含任何值。

 NSValue *value = [NSValue valueWithMKCoordinate:coordinate]; 

谢谢

尝试:

 NSValue *locationValue = [NSValue valueWithBytes:&coordinates[i] objCType:@encode(CLLocationCoordinate2D)]; 

可能是您没有初始化数组。 这就是为什么当你记录它时它是零的。

 _locationsArray = [[NSMutableArray alloc] init];