首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以将返回的CLLocationDistance添加到NSMutableArray中吗?

我可以将返回的CLLocationDistance添加到NSMutableArray中吗?
EN

Stack Overflow用户
提问于 2019-08-28 18:45:23
回答 2查看 126关注 0票数 1

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

ViewController.h

代码语言:javascript
复制
@property (nonatomic) CLLocationDistance kilometers;

ViewController.m

代码语言:javascript
复制
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’的参数

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-28 21:17:10

您必须将其转换为NSNumber

代码语言:javascript
复制
NSMutableArray *a = [NSMutableArray new];
[a addObject:[NSNumber numberWithDouble:self.kilometers]];
票数 0
EN

Stack Overflow用户

发布于 2019-08-28 21:18:41

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

  • 若要将占位符放入可变数组中,请使用NSNull类(而不是尝试将nil放入数组中)。
  • 要将像CGPoint这样的东西放入可变数组中,请使用NSValue类。
  • 要将intdouble这样的原始类型放入可变数组中,请使用NSNumber类。

因此,在您的示例中,由于CLLocationDistance实际上是一个double,所以需要将距离封装在NSNumber对象中,然后将NSNumber对象添加到数组中。要检索距离,请从数组中提取NSNumber对象,并提取double

下面是一个展示整个过程的示例:

代码语言:javascript
复制
// 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 TestSomething1195:476743 2019-08-28 14:02:32.198测试1195:476743距离是2.100000

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57698407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档