在Objective-C中向数组添加空值

时间:2016-01-11 21:06:00

标签: ios objective-c arrays swift null

在一个快速的项目中,我能够轻松地做到这一点。我有一系列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中添加到位置和距离数组时,我得到一个错误,因为我添加了一个没有指针的对象。什么是最好的解决方法?谢谢

目标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 个答案:

答案 0 :(得分:2)

您可以将其存储为CLLocationCoordinate执行此操作:

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

像这样把它拉出来:

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