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;
Related
In my iphone project (ARC enabled) i have a nsmuatble array which contains some 5 managed objects (which are retrieved from core data ) and in some scenario i need to remove all the objects from that nsmutablearray
i have used following methods to remove objects but its crashing in both the cases with the crash log -[__NSArrayI removeObject:]: unrecognized selector sent to instance 0xa391640
if (surveys && [surveys count]>0)
{
[surveys removeAllObjects];
surveys = [[NSMutableArray alloc]init];
}
and also i tried
if (surveys && [surveys count]>0)
{
for(Survey *obj_Survey in surveys)
{
[surveys removeObject:obj_Survey];
}
surveys = [[NSMutableArray alloc]init];
}
can any one tell me how do i empty that array,, any suggestions would be appreciated, thanx in advance
The answer is very simple.
For First case.
Check the part where you have initialized your array, and check if somewhere through your code, if you have assigned an NSArray to your NSMutableArray object.
For second case.
You cannot remove objects from the array while you are enumerating through it. This will result in a crash saying array has mutated while enumerating
This:
[__NSArrayI removeObject:] : unrecognized selector sent to instance 0xa391640
indicates that your array "surveys" is NSArray (not-mutable). Are you sure you have initialized it properly? If you do it by an assignment like this
NSMutableArray* surveys = [someManagedObjectContext executeFetchRequest:request]
an array "surveys" will be of type NSArray because Core Data fetch requests return NSArrays.
The error message says it all,
[__NSArrayI removeObject:]: unrecognized selector
__NSArrayI is a code-word for an immutable array (NSArray), That means your surveys object is not NSMutablArray, but NSArray.
You cannot add or remove objects from NSArray.
Check your code. You have assigned NSArray to surveys or you have reinitialized it as NSArray.
Remove all the things that you are doing and simply do like this
[surveys removeAllObjects];
As from the error that you are getting so maybe that Array is not NSmutableArray just normal array so thats why you can not remove object from It and you try to do so and app got crash
check where you have initialised it if it is Mutable or not
so as I said remove all that thing and use simple removeAllObject
From the exception it's very evident that at some point you are assigning an immutable instance to your mutable array. From the comments I could make out that you were sorting surveys and sorted array was returning immutable array. Type casting will not do good for getting mutableCopy of an immutable object.
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
surveys = (NSMutableArray *) [[[NSMutableArray alloc]initWithArray:surveys ] sortedArrayUsingDescriptors:sortDescriptors];
Instead of it use
[surveys sortUsingDescriptors: #[sortDescriptor1]];
You might have initialised with Immutable Array.
Use the following Code:
NSMutableArray *newArray = [NSMutableArray arrayWithArray: oldArray];
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;
My model class has a NSMutableDictionary with my data. In my view class, I want to display that data in my UITableView cellforRowAtIndexPath method. My view class has an NSArray for the data to display.
I can do this to get my data into my NSArray to display correctly:
self.CategoriesArray = [model.CategoryDictionary allKeys];
However, since NSMutableDictionary does not sort on its own, my items in my TableView are not in alphabetical order. I thought I could do this:
However, I get the following error:
2Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFBoolean localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x13d0a20'
I'm assuming that's because my view class doesn't know what to do with the localizedCaseInsensitiveCompare method.
How do I solve this problem? Thanks.
I think you were saying that this gives you the correct results, just not in order, right?
self.CategoriesArray = [model.CategoryDictionary allKeys];
If that's the case, then this will give you the correct order if I'm understanding your question correctly:
NSArray* keys = [model.CategoryDictionary allKeys];
self.CategoriesArray = [keys sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
localizedCaseInsensitiveCompare: is only to compare NSString, not NSDictionary or NSArray.
To sort your categoriesArray look at
sortedArrayUsingComparator: and sortedArrayUsingDescriptors: for NSArray
sortUsingDescriptors and sortUsingComparator for NSMutableArray
To loop through your array you can do something like
for (NSString *string in categoriesArray)
{
}
I've got a question: I've got an NSMutableArray, which I pass on in multiple views. In one of the views, a value of an object inside that array (NSMutableDictionary) can be editted. I do that using the code:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:selectedItem copyItems:YES];
NSMutableString *newLoc = [[NSMutableString alloc] initWithString:locationField.text];
[tester setValue:newLoc forKey:#"Location"];
[selectedList replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
The problem I'm having with this, is replacing that object in selectedList. It gives the error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0xa631e30'
It works if I copy the new item in a copy-array of selectedList, and alloc selectedList with the new value again, but the other views are having problem finding that same list again in the array (new allocated location).
TL;DR version: how can I edit a value (through replacing?) inside an NSMutableArray? Why doesn't replaceObjectAtIndex work?
EDIT: It was immutable indeed. Still, the main question remains:
I've got:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:geselecteerdItem copyItems:YES];
[geselecteerdItem setValue:nieuweLoc forKey:#"Location"];
[geselecteerdeLijst replaceObjectAtIndex:[geselecteerdeLijst indexOfObject:tester] withObject:geselecteerdItem];
When I'm using: [warningList replaceObjectAtIndex:[warningList indexOfObject:geselecteerdeLijst] withObject:keuzeLijst], it gives me an outofbounds error because the index of the array geselecteerdeLijst obviously changed inside warningList. Any idea's?
selectedList is an immutable array, which doesn't support any modifications. You can do something like this, though:
NSMutableArray *tmp = [NSMutableArray arrayWithArray: selectedList];
[tmp replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
EDIT: To clarify, __NSArrayI is a concrete immutable subclass of NSArray, __NSArrayM is a mutable one. You should not rely on the private class names, but since they speak for themselves, you can at least know which is mutable and which is not.
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.