【问题标题】:CLLocationCoordinate2D converting return nullCLLocationCoordinate2D 转换返回 null
【发布时间】:2013-04-28 18:48:07
【问题描述】:

我正在尝试将经度和纬度转换为 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)

任何线索它返回?

【问题讨论】:

    标签: iphone objective-c xcode cllocationmanager nsnumber


    【解决方案1】:

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

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

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

    【讨论】:

    • 关于 nil 的好提示..我在视图之间传递数据,我的 DTO 对象没有初始化..你的提示很好,你拯救了我的夜晚并接受了;)
    猜你喜欢
    • 2019-05-23
    • 2013-12-28
    • 2019-06-22
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多