Ios 我可以将返回的CLLocationDistance添加到NSMutableArray中吗?
我正在使用下面的代码体查找登录用户和我应用程序数据库中其他用户列表之间的距离 ViewController.hIos 我可以将返回的CLLocationDistance添加到NSMutableArray中吗?,ios,objective-c,Ios,Objective C,我正在使用下面的代码体查找登录用户和我应用程序数据库中其他用户列表之间的距离 ViewController.h @property (nonatomic) CLLocationDistance kilometers; ViewController.m NSString *myLocation = self.currentUser[0][@"street_address"]; NSLog(@"MY LOCATION %@", myLocation);
@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.km添加到NSMutableArray中?当我尝试这样做时,XCode会抛出一个错误,告诉我:
正在向的参数发送“CLLocationDistance”(也称为“double”)
不兼容的类型“id\u Nonnull”
您必须将其转换为
NSNumber
NSMutableArray*a=[NSMutableArray new];
[a addObject:[NSNumber numberWithDouble:self.km]];
您必须将其转换为NSNumber
NSMutableArray*a=[NSMutableArray new];
[a addObject:[NSNumber numberWithDouble:self.km]];
一个NSArray
或NSMutableArray
中的条目都必须是对象指针<不允许使用code>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);
以下是输出:
测试某物[1195:476743](“2.1”
)
测试某物【1195:476743】距离是2.100000
NSArray
或NSMutableArray
中的条目都必须是对象指针<不允许使用code>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);
以下是输出:
测试某物[1195:476743](“2.1”
)
测试某物【1195:476743】距离是2.100000
经验法则是不能将基本数据类型添加到数组中。数组是指针,可以存储对象
CLLocationDistance
是一个double
(这是基本数据类型),因此在添加到数组之前,必须将其转换为object。经验法则是不能将基本数据类型添加到数组中。数组是指针,可以存储对象CLLocationDistance
是一个double
(这是基本数据类型),因此在添加到数组之前,必须将其转换为object。