CLLocationCoordinate2D转换返回null

时间:2013-04-28 18:48:08

标签: iphone objective-c xcode cllocationmanager nsnumber

我试图将longituude和latitude转换为NSNumbers以将它们保存在核心数据中。

关注我的代码:

获得坐标

CLLocationCoordinate2D coordinate = [[mapView.annotations objectAtIndex:0] coordinate];
    NSLog(@"Long2: %f", coordinate.longitude);
    NSLog(@"Lat2: %f", coordinate.latitude);

输出

2013-04-28 20:22:05.918 [1892:16a03] Long2: -122.406417
2013-04-28 20:22:05.918 [1892:16a03] Lat2: 37.785834

设置值

[members.eventDTO setLatLong:coordinate];
- (void)setLatLong: (CLLocationCoordinate2D)coordinate{
    self.locLatitude = [NSNumber numberWithDouble:coordinate.latitude];
    self.locLongitude = [NSNumber numberWithDouble:coordinate.longitude];
}

然后我尝试输出:

NSLog(@"Long2: %f", [members.eventDTO.locLongitude doubleValue]);
NSLog(@"Lat2: %@", members.eventDTO.locLatitude);

2013-04-28 20:22:05.918 [1892:16a03] Long2: 0.000000
2013-04-28 20:22:05.919 [1892:16a03] Lat2: (null)

它返回的任何线索?

1 个答案:

答案 0 :(得分:0)

根据您在问题中描述的内容,在您使用setLatLong:方法设置对象后,该对象似乎仍为零。我在这里建议的提示是:

  • 尝试在您的setLatLong:方法中记录NSNumber,以检查它们是否正确创建;
  • 尝试在设置后记录self.locLatitude和self.locLongitude,看看它们是否被正确分配。
  • 是否正确创建了NSManagedObject?
  • 你是否正在调用save给你的NSManagedObjectContext?

请发布更多信息,以便我们可以一起解决问题;)

相关问题