Search code examples
iphoneiosclgeocodermkmapitem

Unrecognized selector error processing results from geocodeAddressString


I'm trying to create multiple placemarks using MKMapItem without using coordinates.

I used location name directly in geocodeAdressString:@"Mumbai"... but I got result for single location.

While I use multiple locations through array, I'm getting this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'

Why is this problem occurring?

Class mapItemClass=[MKMapItem class];
if(mapItemClass &&[mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
NSArray *addr=[[NSArray alloc ]initWithObjects:@"Banglore",@"Mumbai",@"Delhi", nil];

    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *geocodedPlacemark=[placemarks objectAtIndex:0];
        MKPlacemark *placemark=[[MKPlacemark alloc]initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];
        MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:placemark];
        [mapItem setName:geocodedPlacemark.name];


        [MKMapItem openMapsWithItems:@[mapItem] launchOptions:nil];

    }];

}

Solution

  • The error state that -[__NSArrayI length]: unrecognized selector sent to instance 0xab48380' NSArray dont have a property length. So it is unable to find the selector of length. So check where you are using NSArray and keep break points to find where error is happening. length is the method of NSString and NSData but NSArray dont have length, it has count