【问题标题】:Can I add returned CLLocationDistance into an NSMutableArray?我可以将返回的 CLLocationDistance 添加到 NSMutableArray 中吗?
【发布时间】:2019-08-28 18:45:23
【问题描述】:

我正在使用以下代码体来查找登录用户与我的应用数据库中其他用户列表之间的距离。

ViewController.h

@property (nonatomic) CLLocationDistance kilometers;

ViewController.m

NSString *myLocation = self.currentUser[0][@"street_address"];
                NSLog(@"MY LOCATION %@", myLocation);


                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                [geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error){
                    for (CLPlacemark* aPlacemark in placemarks2)
                    {

for (NSMutableDictionary *multiplelocationsFriend in self.closeByNeighbours) {

NSString *location = [multiplelocationsFriend objectForKey:@"address"];

CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                            [geocoderFriend geocodeAddressString:location
                                               completionHandler:^(NSArray* placemarks, NSError* error){
                                                   if (placemarks && placemarks.count > 0) {
                                                       CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                                       MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];


                                                         self.endLocation = [[CLLocation alloc] initWithLatitude:placemark.location.coordinate.latitude longitude:placemark.location.coordinate.longitude] ;


                                                        NSString *myLocation = self.currentUser[0][@"street_address"];
                                                        NSLog(@"MY LOCATION %@", myLocation);

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error){
                                                        for (CLPlacemark* aPlacemark in placemarks2)
                                                        {
 self.startLocation = [[CLLocation alloc] initWithLatitude:aPlacemark.location.coordinate.latitude longitude:aPlacemark.location.coordinate.longitude] ;

                                                       self.kilometers = [self.startLocation distanceFromLocation:self.endLocation] / 1000;


                                                        }
                                                        }];


                                                   }
                                               }
                             ];
                        }

                    }
                }];




                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.neighboursView reloadData];
                });



            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Failure: %@", [error localizedDescription]);
            }];

有没有办法将 self.kilometers 添加到 NSMutableArray 中?当我尝试这样做时,XCode 向我抛出一个错误:

将“CLLocationDistance”(又名“double”)发送到参数 不兼容的类型'id _Nonnull'

【问题讨论】:

  • 经验法则是您不能将原始数据类型添加到数组中。数组是指针,可以存储对象。 CLLocationDistancedouble (这是原始数据类型),因此在添加到数组之前,您必须将其转换为对象。

标签: ios objective-c


【解决方案1】:

您必须将其转换为NSNumber

NSMutableArray *a = [NSMutableArray new];
[a addObject:[NSNumber numberWithDouble:self.kilometers]];

【讨论】:

    【解决方案2】:

    NSArrayNSMutableArray 中的条目必须都是对象指针。不允许使用nil 指针和非对象类型(例如intdoubleCGPoint)。

    • 要将占位符放入可变数组,请使用NSNull 类 (而不是尝试将 nil 放入数组中)。
    • 要将CGPoint 之类的内容放入可变数组中,请使用NSValue 类。
    • 要将intdouble 等原始类型放入可变数组中, 使用NSNumber 类。

    所以在你的情况下,由于CLLocationDistance实际上是一个double,你需要将距离封装在一个NSNumber对象中,然后将NSNumber对象添加到数组中。要检索距离,请从数组中获取NSNumber 对象,然后提取double

    这是一个展示整个过程的例子:

    // create a mutable array
    NSMutableArray *array = [NSMutableArray new];
    
    // encapsulate a CLLocationDistance (actually a 'double') in a NSNumber, and add the NSNumber to the array
    CLLocationDistance distance1 = 2.1;
    NSNumber *number1 = [NSNumber numberWithDouble:distance1];
    [array addObject:number1];
    NSLog(@"%@", array);
    
    // get the NSNumber from the array and unpack the 'double' value
    NSNumber *number2 = [array firstObject];
    CLLocationDistance distance2 = [number2 doubleValue];
    NSLog(@"the distance is %lf", distance2);
    

    这是输出:

    2019-08-28 14:02:32.197 TestSomething[1195:476743] (
    《2.1》
    )
    2019-08-28 14:02:32.198 TestSomething[1195:476743] 距离为 2.100000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      相关资源
      最近更新 更多