我正在使用下面的代码来查找登录用户与应用程序数据库中其他用户列表之间的距离。
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’的参数
发布于 2019-08-28 21:17:10
您必须将其转换为NSNumber。
NSMutableArray *a = [NSMutableArray new];
[a addObject:[NSNumber numberWithDouble:self.kilometers]];发布于 2019-08-28 21:18:41
NSArray或NSMutableArray中的条目都必须是对象指针。不允许nil指针和非对象类型(例如int、double、CGPoint) .
NSNull类(而不是尝试将nil放入数组中)。CGPoint这样的东西放入可变数组中,请使用NSValue类。int和double这样的原始类型放入可变数组中,请使用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 TestSomething1195:476743 2019-08-28 14:02:32.198测试1195:476743距离是2.100000
https://stackoverflow.com/questions/57698407
复制相似问题