-->

problems converting data in cllocationdistance

2019-09-05 02:51发布

问题:

Im trying to add to a mutable array and can not figure this out. Throws error for incompatable data types double to id. question is how do i convert distance and store it in my array.

for (int i = 0; i < _getAllLocations.count; i++) {

uscInfo *entity = [_getAllLocations objectAtIndex:i];
NSNumber *numlat=[[NSNumber alloc] initWithDouble:[entity.Latitude doubleValue]];
NSNumber *numlon=[[NSNumber alloc] initWithDouble:[entity.Longitude doubleValue]];
NSLog(@"%d",[_getAllLocations count]);
la=[numlat doubleValue];
lo=[numlon doubleValue];
CLLocation *currentLocation =  [[CLLocationalloc]initWithLatitude:lalongitude:lo];////here put your destination point

//NSLog(@"New Location:%@", newLocation);
CLLocationDistance distance = [newLocation distanceFromLocation:currentLocation];
NSLog(@"%@ (%@) %4.0f km from current location",entity.EntityNo, entity.EntityName, distance);
NSLog(@"%@",currentLocation);

I am trying to store "distance" in my array "locationDistance"

thanks for any help, I am still new to iOS as well as programming in general.

回答1:

(You really should include the code that's not working - in this case the actual addObject: code.)

But I can tell you that the problem is you are likely trying to add a naked CLLocationDistance to the array, which won't work as it's not an object. It's typedef'd as a double so you need to wrap it using, e.g., [NSNumber numberWithDouble:distance].