Unrecognized selector error processing results from geocodeAddressString - iphone

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];
}];
}

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

I have been able to replicate this using this code
id array = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSLog(#"%d",[array length]);
output
-[__NSArrayI length]: unrecognized selector sent to instance 0x96bafe0
notice how I have used id instead of NSArray with using id I am able to call length which isn't allowed by NSArray, but this gets round the compiler when using id.
So the best way to find out where this is going wrong is add an exception that will catch all exceptions. Do this by selecting the exceptions tab in the project navigator wind >> select '+' >> 'Add Exception Breakpoint...' >> then just select done. This will set a breakpoint every time your app throws an exception.
EDIT
Thanks to the code you have added I suspect that you are passing an NSArray where there should be an NSString. You create
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
then pass it to geocodeAddressString:addr
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
Just from the name of this parameter I suspect it should be an NSString and not an NSArray try replacing addr to [addr objectAtIndex:0] this will get the string object at index 0 of the addr array.
EDIT 2
Here is the method you are calling notice it only allows an NSString to be passed in for geocodeAddressString.
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

Related

NSdata length crash on device

While running this code:
NSData *archivedSavedData = [[NSData alloc] init];
archivedSavedData = [defaults objectForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData length] );
I am getting this crash error (last line) only when running on a device that is connected:
[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0
2012-03-13 20:25:33.088[7301:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0'
* First throw call stack:
(0x34dbc88f 0x361e3259 0x34dbfa9b 0x34dbe915 0x34d19650 0xccb1b 0x31e13e33 0x31e38629 0x31dfcd7d 0x31ebf4dd 0x31e0555d 0x31e05579 0x31e0540b 0x31e053e7 0xcfedf 0x31e12e53 0x31e0c985 0x31ddac6b 0x31dda70f 0x31dda0e3 0x3600f22b 0x34d90523 0x34d904c5 0x34d8f313 0x34d124a5 0x34d1236d 0x31e0ba13 0x31e08e7d 0xcfd39 0xcbe28)
terminate called throwing an exception
This doesn't happen when running on the simulator or directly on the device with a distribution profile (through testflight for example).
Does anyone know how such a behavior could happen only in this case?
Thanks.
UPDATE: when trying to replace length with count I get this complication error: "No visible #interface for 'NSData' declares the selector 'count'"
UPDATE2: I understand that it should be an NSArray rather than an NSData, but my problem is that I did store archived NSData cause my array consists of custom objects, so I had to archived this data into NSData format when saving in NSUserDefault. How else should I approach that otherwise?
Thats how I store the data:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
[defaults setObject:data forKey:#"listOfAccessNumbers"];
array is an array of custom objects of the form of:
#interface NumberDataObj : NSObject {
NSString *inputName;
NSString *inputNum;
}
The error message says:
-[__NSCFArray length]: unrecognized selector sent to instance
That means that archivedSavedData is an array and that it doesn't (obviously) respond to length so you should declare archivedSavedData as an array and use count instead.
NSArray *archivedSavedData = [defaults objectForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData count]);
Now, as to why this doesn't happen when running on the simulator, my guess is that your test scenarios don't make this part of the code get called.
EDIT
If you want to retrieve the data as NSData then use the method dataForKey:
NSData *archivedSavedData = [defaults dataForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData length]);
The documentation says for dataForKey:
Return Value
The data object associated with the specified key, or nil if the key does not exist or its value is not an NSData object.
and for arrayForkey:
Return Value
The array associated with the specified key, or nil if the key does not exist or its value is not an NSArray object.
So aways use the appropriate method when you know the type of the data to avoid problems like this.
You have two problems as the code is written:
1) You allocate a variable called archivedSavedData that you reassign on the following line without releasing. This is okay if you are working with ARC, but the first line would then be unnecessary.
2) The second problem is that the object corresponding to the key #"listOfAccessNumbers" stored in your defaults object is of type NSArray. NSArray responds to the selector count, not length. Maybe you should look more closely at this object and recode accordingly.
Hope this helps :)
It's because [defaults objectForKey:#"listOfAccessNumbers"] returns an NSArray, not an NSData object.

Initializing NSMutableDictionary to create a filter

This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary
(It's probably a stupid question, but I don't see what I'm doing wrong.)
I liked the idea of just having "All" listed in my TableView that pops up when the filter button is pressed in order to indicate no filter applied. I get my data from my model class sorted correctly into an NSMutableArray like so:
NSArray *anArray = [dmgr.CategoryDictionary allKeys];
self.CategoriesArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
[self.CategoriesArray insertObject:#"All" atIndex:0]; // line causes crash
But I want to add the "All" option to the CategoriesArray to show in the UITableView. When I try to run it, it crashes at that line because of
NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0'
Any help would be appreciated. Thanks!
sortedArrayUsingSelector: returns an immutable array which is why you can't add to it. Do this to get a mutable copy.
self.CategoriesArray = [NSMutableArray arrayWithArray:[anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]];
It seems CategoriesArray is actually a NSArray instance. From Apple's doc :
-(NSArray *)sortedArrayUsingSelector:(SEL)comparator;

problem in parsing JSON response in iphone

i am getting json data from this link
now i want data from "html_instructions": part
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
NSString *string;
for(NSDictionary *di in resultarr){
NSLog(#"for loop");
string = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
but after printing "for loop" in console it is throwing an exception
2011-05-20 16:16:26.997 speedymap[759:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60
2011-05-20 16:16:26.999 speedymap[759:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60'
please help me
i want html_instructiuons field value
Check if resultarr and di really contains anything or not.
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
CFShow(resultarr);
NSString *string;
for(NSDictionary *di in resultarr){
CFShow(di);
NSLog(#"for loop");
NSArray *tempArr = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
http://jsonviewer.stack.hu/#http://maps.googleapis.com/maps/api/directions/json?origin=delhi&destination=noida&waypoints=&sensor=true
Check the viewer and set the values accordingly. [di objectForKey:#"legs"] returns an array. the first object of that array is a dictionary which has the key steps. But that too returns another array.

NSCFArray length]: error, array regex

StringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
//Regex Out Artist Name
//NSString *regEx = ;
NSArray *iTunesAristName = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([iTunesAristName isEqual:#""]) {
NSLog(#"Something has messed up");
//Regex Out Song Name
}else{
NSLog(iTunesAristName);
}
NSLog(iTunesAristName);
[stringReply release];
I just keep getting this error ?
2010-09-29 21:15:16.406 [2073:207] *** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800
2010-09-29 21:15:16.406 [2073:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800'
2010-09-29 21:15:16.407 [2073:207] Stack: (
please help its driving me crazy
The first argument to NSLog is supposed to be a format string. You're passing an NSArray. When the function tries to treat your array as a string, you get that error. Instead, use NSLog(#"%#", iTunesAristName);.
Chuck has answered your question, but I've noticed something else that is problematic.
NSArray is an array, not a string, so [iTunesArtistName isEqual:#""] will never return true, because they are different classes. Even if iTunesArtistName was a string, it should be compared using the isEqualToString: method, not isEqual:.
If you want to extract only the artist's name, you might be able to do this:
NSArray *matches = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([matches count] == 0)
{
NSLog(#"Could not extract the artist name");
}
else
{
NSString *iTunesArtistName = [matches objectAtIndex:0];
NSLog(#"Artist name: %#", iTunesArtistName);
}
I see you're using RegexKitLite, make sure you import libicucore.dylib, i was getting the same error until i imported that library.

Crash when zooming into a MKMapView with a MKPlacemark

I'm trying to add a placemark to a map. The placemark is built from an address completely outside of Address Book.
My placemark is appearing on the map, but when I try to pinch to zoom in I get a crash:
*** -[CALayer objectForKey:]: unrecognized selector sent to instance 0x4569dc0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CALayer objectForKey:]: unrecognized selector sent to instance 0x4569dc0'
Here's how I'm setting up the address:
id theAddress = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat: #"%# - %#", theAddress1 ? theAddress1 : #"", theAddress2 ? theAddress2 : #""], kABPersonAddressStreetKey,
theCity ? theCity : #"", kABPersonAddressCityKey,
theState ? theState : #"", kABPersonAddressStateKey,
theZip ? theZip : #"", kABPersonAddressZIPKey,
theCountry ? theCountry : #"", kABPersonAddressCountryKey,
nil];
I use the values in the address record to find the coordinate for the address (learned how to do this from this question & answer), then I add it to my map:
[mapView addAnnotation: [[[MKPlacemark alloc] initWithCoordinate: theCoordinate
addressDictionary: theAddress] autorelease]];
The crash definitely seems to be caused by this MKPlacemark, as if I comment out the addAnnotation statement the code doesn't crash.
Any idea what's going on? I'm guessing I haven't got enough in the address record, but the error message is really unhelpful.
You're over-releasing an NSDictionary object somewhere. Here's how you find it:
Under the Project menu, select 'Edit Active Executable'. In the Arguments tab, add an item to the "Variables to be set in the environment:" block with the name NSZombieEnabled and the value YES.
Happy Zombie Hunting.
Is something happening to that theAddress NSDictionary between when you create it and when you pass it to MKPlacemark's init method? I ask because it seems like you're crashing because your code is trying to treat a CALayer object like it's a collection class (that is, like it has an objectForKey: method), and the only collection class I see in there is that address dictionary.