How to avoid "NSInternalInconsistencyException" in iPhone? - 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.

Related

NSInvalidArgumentException while adding NSDictionary to NSArray

I have declared NSDictionary *dict; globally and trying to add this dictionary into array but getting an Error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My Code :
dict = #{#"Testcase" : Testcase, #"TestResult" : TestResult,#"Message": Message};
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
How to solve this Error ?
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[__NSArrayM
insertObject:atIndex:]: object cannot be nil'
Problem : From the above ErrorMessage, it is clear that you are trying to add NULL value in your NSDictionary.
Explanation : At the time of inserting Value to your NSDictionary , note that the Value shouldn't be NULL and Non-String. You are trying to add Value of Variable to your Dictionary directly. Instead you should try to add like this :
Answer :
[NSString stringWithFormat:#"%#",Testcase];
Benefits :
It will add String-Value.
It will add Empty-Value when your Value of Variable is NULL.
dict = [NSDictionary dictionaryWithObjectsAndKeys:Testcase:#"Testcase", TestResult: #"TestResult",Message, #"Message", nil];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
Make sure TestcaseId is not more than your array count
[myArray addobject:dict];
or
[myArray replaceObjectAtIndex:TestcaseId withObject:dict]

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;

NSSortDescriptor

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".

NSMutuableArray sort problem

NSMutableArray *labels;
and it is properly populated.
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"self" ascending: NO];
labels = [labels sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance.
Even if I change NSArray to NSMutuableArray in the last line, I still get the error.
Thanks
dont assign the same array to it.if it is nsarray which is immutable.
so do like this
put ur
labels as NSMutableArray
NSMutableArray *resultArray;
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"self" ascending: NO];
resultArray = [[labels sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]] mutableCopy];
self.labels=resultArray;//updated code
NSLog(#"resultArray : %# \n\n",resultArray);
NSArray * descriptors = [NSArray arrayWithObjects: sortOrder, nil];
labels = [labels sortedArrayUsingDescriptors:descriptors];
This should do the trick for you.

Exception when sorting NSMutableArray which contains all NSDate objects

//list has type of NSMutableArray
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES] autorelease];
[list sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
I got this exception:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDate 0x5d57980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key date.'
Any idea? How to fix it?
An NSDate does not have the -date method, so your NSSortDescriptor won't work.
Why not just use
[list sortUsingSelector:#selector(compare:)];
?