1

I want to add latitude and longitude value to NSMutableArray from geocoder value but I get an error in the image .

-(void) loadDataArray:(NSString *) text{

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:[NSString stringWithFormat:@"%@",text] completionHandler:^(NSArray* placemarks, NSError* error){


    for (CLPlacemark* aPlacemark in placemarks)
    {
        NSLog(@"aPlace: %@",aPlacemark.country);
        NSLog(@"MyPlace: %@",userCountry);
        //if ([aPlacemark.country isEqual:userCountry]) {

            [dataArray addObject:aPlacemark];
        //NSNumber *lat = [NSNumber numberWithDouble:aPlacemark.location.coordinate.latitude];
        //NSNumber *lon= [NSNumber numberWithDouble:aPlacemark.location.coordinate.longitude];


        [dataArrayLat addObject:aPlacemark.location.coordinate.latitude];
        [dataArrayLon addObject:aPlacemark.location.coordinate.longitude];

            //NSLog(@" %@",lat);
        //}
        //else{
          //  NSLog(@"Bulunamadı");

        //}

    }
    [searchTable reloadData];
}];

}

enter image description here

Thanks in replies.

EDIT

I want to use for search location and show tableview.I select tableview item and then location show on Apple Map.

fozoglu
  • 729
  • 5
  • 21
  • Why not just keep a reference to the whole `placemarks` array itself instead of two separate arrays containing just numbers which no longer tell you what place the number is referring to? Related: http://stackoverflow.com/a/24932858/467105 –  Jan 10 '15 at 14:44
  • @Anna I agree. I was trying to tell him why his code had an error, but your approach is the better way. ;-) – mbm29414 Jan 10 '15 at 14:52

1 Answers1

1

You're trying to add a primitive to an NSMutableArray, but it will only hold objects.

The quickest workaround would be to wrap your CLLocationDegrees value (which is just a double) in an NSNumber, like this:

[dataArrayLat addObject:@(aPlacemark.location.coordinate.latitude)];
[dataArrayLon addObject:@(aPlacemark.location.coordinate.longitude)];

Keep in mind though, that you'll have to unwrap the value later if you want to access the double again. You would do so like this:

NSNumber *num = dataArrayLat[someIndex];
double latitude = num.doubleValue;
mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • I tried this code.I controled value with NSLog which value is null. – fozoglu Jan 10 '15 at 14:20
  • Add `NSLog(@"%@", dataArrayLat);` immediately after the first `addObject:` line. Does it print contents then? You likely aren't accessing the array correctly when you are trying to retrieve lat/long objects. – mbm29414 Jan 10 '15 at 14:41
  • Also, you might be better off to have just one array that contains the location objects, rather than splitting latitude and longitude into separate arrays. That's probably more efficient, but I was answering your original question. – mbm29414 Jan 10 '15 at 14:42
  • I add "NSLog(@"%@", dataArrayLat); " under array variable but null return. How can I use efficently Lat, Lon variable. – fozoglu Jan 10 '15 at 14:49
  • I would store the location objects in an array, and then you have correctly associated data at any time you try to access it in the future. What is your purpose for storing these values? – mbm29414 Jan 10 '15 at 14:51
  • I want to use for search location and show tableview.I select tableview item and then location show on Apple Map. – fozoglu Jan 10 '15 at 14:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68549/discussion-between-fozoglu-and-mbm29414). – fozoglu Jan 10 '15 at 15:00
  • Worked, Thanks... I forget add dataArrayLat =[[NSMutableArray alloc]init]; dataArrayLon = [[NSMutableArray alloc]init]; – fozoglu Jan 10 '15 at 15:47
  • Yeah, adding an object to a nil array will "work" (in that it doesn't crash) but "fail" (in that you don't get your desired outcome). – mbm29414 Jan 10 '15 at 16:00