在一個快速項目中,我能夠輕鬆完成此任務。我有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];
}
爲什麼不只是將'requestCLLocation'添加到'self.locations'並從中拉出'CLLocationCoordinate2D'當你需要它? – random
標題中指定的問題中沒有'null'。 – Cristik