Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Here's the deal. I'm trying to have the app remove an object from a mutable array while it is looping through another array. The object to be removed and the array that the object is to be removed from are both defined. However, I just got the following error message:

Code:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x95e6160
 
Here's the deal. I'm trying to have the app remove an object from a mutable array while it is looping through another array. The object to be removed and the array that the object is to be removed from are both defined. However, I just got the following error message:

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

Sounds like the array is not mutable. How are you creating it? Note that if you save a mutable array to a plist or use NSCoding to save it, when you read it back, it's immutable.
 
Here's the deal. I'm trying to have the app remove an object from a mutable array while it is looping through another array. The object to be removed and the array that the object is to be removed from are both defined. However, I just got the following error message:

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

You don't have a mutable array. Show us your code from the point where the array is created until you attempt to remove an object from it.
 
Here's what my code looks like

Code:
NSMutableArray *array1 = (NSMutableArray *)[array2 arrayByAddingObject:object];
for (object in array3)
{
[array1 removeObject:definedObject];
}


The app crashes before it runs out of objects in array1.
 
NSArray's arrayByAddingObject: returns an NSArray. Just because you typecast it as a NSMutableArray does not make it mutable. I would look into using mutableCopy.
 
Update: After reading the Stack Overflow page entitled "Error: Mutating method sent to immutable object for NSMutableArray from JSON file," I figured out that simply casting an NSArray to an NSMutableArray does not produce an array that can be changed. I have to create a mutable copy of the immutable array and assign that array to the mutable array I mentioned in the post above.

Edit:
NSArray's arrayByAddingObject: returns an NSArray. Just because you typecast it as a NSMutableArray does not make it mutable. I would look into using mutableCopy.

Thanks. I did not notice your post until now.
 
NSMutableArray doesn't have its own arrayWithArray: method, so that would still call the superclass NSArray's method (of which NSMutableArray is a subclass) and so the return value is still an (immutable) NSArray.
This.

Also I suggest not using fast enumeration to add and/or remove objects.
 
NSMutableArray doesn't have its own arrayWithArray: method, so that would still call the superclass NSArray's method (of which NSMutableArray is a subclass) and so the return value is still an (immutable) NSArray.

I have found multiple sources that state using arrayWithArray: to get a NSMutableArray from a NSArray. I feel like I have also used it before with fine results. Am I doing something wrong here? I went back and read documentation and it seems you are right that arrayWithArray is just a method from the superclass, but I still feel like it worked for me in the past.
 
I have found multiple sources that state using arrayWithArray: to get a NSMutableArray from a NSArray. I feel like I have also used it before with fine results. Am I doing something wrong here? I went back and read documentation and it seems you are right that arrayWithArray is just a method from the superclass, but I still feel like it worked for me in the past.

Huh. Interesting. Well, I might be wrong here. A simple code test should be all it takes to verify whether that works or not. I'll let you know what I find.
 
arrayWithArray: should return an NSMutableArray, I believe, when invoked on an NSMutableArray. The method signature says that it returns an NSArray, which an NSMutableArray qualifies as.

No, I didn't actually take the 5 minutes to set up a test for this.
 
Huh. Interesting. Well, I might be wrong here. A simple code test should be all it takes to verify whether that works or not. I'll let you know what I find.

I just tested it, and the NSMutableArray version of arrayWithArray does create a new mutable array.

The NSArray class reference defines NSArray arrayWithArray: as

Code:
+ (instancetype)arrayWithArray:(NSArray *)anArray

That implies, in a very ambiguous fashion, that you get back an object of the type you created.
 
Thanks for the update, Duncan. I guess that settles it. My apologies for possibly leading the OP astray.

The docs for NSMutableaArray should list a mutable array variant of the arrayWithArray method.

----------

I just tested it, and the NSMutableArray version of arrayWithArray does create a new mutable array.

The NSArray class reference defines NSArray arrayWithArray: as

Code:
+ (instancetype)arrayWithArray:(NSArray *)anArray

That implies, in a very ambiguous fashion, that you get back an object of the type you created.


What does the type "instancetype" mean, anyway? Type of the target?
 
What does the type "instancetype" mean, anyway? Type of the target?

yes, as far as I recall instancetype means type of the target, and is used a lot in custom init methods instead of specifying -(id) return type

Does this then mean that if you invoke it with [NSMutableArray arrayWithArray:someArray] it returns the type of NSMutableArray

EDIT: What is interesting is that in the class reference for NSArray, under the method +(instancetype)array there is a note that states

Discussion

This method is used by mutable subclasses of NSArray.

However that is not present for +(instancetype)arrayWithArray
 
yes, as far as I recall instancetype means type of the target, and is used a lot in custom init methods instead of specifying -(id) return type

Does this then mean that if you invoke it with [NSMutableArray arrayWithArray:someArray] it returns the type of NSMutableArray

EDIT: What is interesting is that in the class reference for NSArray, under the method +(instancetype)array there is a note that states



However that is not present for +(instancetype)arrayWithArray


Yes, when used like this:

Code:
  NSArray *anArray = @[@"one", @"two", @"three"];
  NSMutableArray *aMut = [NSMutableArray arrayWithArray: anArray];

The result is a mutable array.
 
yes, as far as I recall instancetype means type of the target, and is used a lot in custom init methods instead of specifying -(id) return type

Does this then mean that if you invoke it with [NSMutableArray arrayWithArray:someArray] it returns the type of NSMutableArray

EDIT: What is interesting is that in the class reference for NSArray, under the method +(instancetype)array there is a note that states



However that is not present for +(instancetype)arrayWithArray
Please report the doc-error to Apple, using the on-page feedback.

Doc bugs are bugs. Report them so they can be fixed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.