NSSortDescriptor - iphone

Please give me suggestion, How can I solve solve this problem..
 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Weight" ascending:NO selector:#selector(localizedStandardCompare:)];
 NSArray *sortedArray = [arrayToSort sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
I am getting output :
-[__NSCFNumber length]: unrecognized selector sent to instance 0x6a81cf0
2012-05-16 09:54:21.480 MedzioSearch[2188:f803] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[__NSCFNumber length]: unrecognized selector sent to instance 0x6a81cf0

What kind of objects are in arrayToSort? What is the type of their "Weight" property?
At a guess, some of the objects have a Weight property which is an NSString while some have a Weight property which is an NSNumber. As a consequence, the sort is trying to do something like [someString localizedStandardCompare:someNumber]. Internal to -[NSString localizedStandardCompare:], it's invoking -length on the argument which is an NSNumber and doesn't recognize that selector.
By the way, property names should begin with a lowercase letter unless they begin with an acronym or initialism (like "URL" or "TIFF"). So, your property should be named "weight", not "Weight".

Related

NSInvalidArgumentException', reason: '-[__NSArrayM name]: unrecognized selector sent to instance 0x75786f0'

CellData *cellData = [self.tableElements objectAtIndex:indexPath.row];
NSLog(#"name is %#",[self.tableElements objectAtIndex:1]);
cell.lbl.text = cellData.name;
here am getting the error when i able to access my NSObject class variables.
self.tableElements is an array with elements parsed.
cellData is not what you think it is, you think it is a CellData object but it is actually an NSArray, double check what self.tableElements is holding.

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

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;

How to avoid "NSInternalInconsistencyException" in iPhone?

I am sorting an mutable array. For sorting I use:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"pubDate" ascending:NO];
[recent sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
[descriptor release];
I am getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
The line
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
show warnings
"passing argument 1 of 'sortedarrayusingdescritors' from distinct objective c type " and
"assignment from distinct objective c type"
In my code, both recent and recent1 are NSMutable arrays. Where do I go wrong?
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
must be:
recent1 = [recent sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
Though I have no idea why you would want to sort an array that you already sorted with the same sort descriptors on the line directly above.

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.