【发布时间】: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'
【问题讨论】:
-
经验法则是您不能将原始数据类型添加到数组中。数组是指针,可以存储对象。
CLLocationDistance是double(这是原始数据类型),因此在添加到数组之前,您必须将其转换为对象。
标签: ios objective-c