【发布时间】:2016-01-11 21:05:59
【问题描述】:
在一个快速的项目中,我能够轻松地做到这一点。我有一组 CLLocationCoordinate2D 和 CLLocationDistances
这是 swift 代码(在 PFQuery 中)它工作得很好
if let returnedLocation = object["location"] as? PFGeoPoint
{
let requestLocation = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude)
self.locations.append(requestLocation)
let requestCLLocation = CLLocation(latitude: requestLocation.latitude, longitude: requestLocation.longitude)
let driverCLLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
let distance = driverCLLocation.distanceFromLocation(requestCLLocation)
self.distances.append(distance/1000)
}
当我尝试在 Objective C 中将它们添加到位置和距离数组时出现错误,因为我正在添加一个没有指针的对象。解决这个问题的最佳方法是什么?谢谢
Objective C 代码(它们都是 NSMutableArrays)
if (object[@"driverResponded"] == nil) {
NSString *username = object[@"username"];
[self.usernames addObject:username];
PFGeoPoint *returnedLocation = object[@"location"];
CLLocationCoordinate2D requestLocation = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude);
//FIX!
[self.locations addObject:requestLocation];
CLLocation *requestCLLocation = [[CLLocation alloc]initWithLatitude:requestLocation.latitude longitude:requestLocation.longitude];
CLLocation *driverCLLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
CLLocationDistance distance = [driverCLLocation distanceFromLocation:requestCLLocation];
[self.distances addObject:distance/1000];
}
【问题讨论】:
-
为什么不将
requestCLLocation添加到self.locations并在需要时从中提取CLLocationCoordinate2D?? -
标题中指定的问题中没有
null。
标签: ios objective-c arrays swift null