2016-01-11 18 views
-2

在一个快速项目中,我能够轻松完成此任务。我有CLLocationCoordinate2D的数组和CLLocationDistances将null值添加到Objective-C中的数组中

这里是SWIFT代码(PFQuery内)当我尝试在目标C将它们添加到我得到的位置和距离阵列它工作得很好

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的代码(他们都是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]; 
} 
+1

为什么不只是将'requestCLLocation'添加到'self.locations'并从中拉出'CLLocationCoordinate2D'当你需要它? – random

+2

标题中指定的问题中没有'null'。 – Cristik

回答

2

为此,您可以将其保存为CLLocationCoordinate这样做:

CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude); 
[self.locations addObject:[NSValue valueWithMKCoordinate:new_coordinate]]; 

拉出它是这样的:

CLLocationCoordinate2D coordinate = [[self.locations objectAtIndex:0] MKCoordinateValue]; 
+2

谢谢!距离[self.distances addObject:[NSNumber numberWithDouble:distance]];似乎也工作。 – Echizzle

+0

很高兴帮助!确保正确标记答案,以便其他人不要提交新答案! – random