2016-01-11 17 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