-3

self.filteredProducts = [[NSMutableArray alloc] init];

    //Find products
    self.filteredProducts = (NSMutableArray*)[(NSMutableDictionary *)[self.CECategoriesDictionary objectForKey:category.categoryGuid] allValues];

    if([self.oldfilteredProducts count] > 0 && selectedLevel==5)
    {
        NSMutableArray *diff = [self.filteredProducts mutableCopy];
        [diff removeObjectsInArray:[NSArray arrayWithArray:self.oldfilteredProducts]];
        [self.filteredProducts removeObjectsInArray:[NSArray arrayWithArray:diff]];
    }

Logs :

[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0 2014-09-15 22:00:19.243 SAPRetailEx[10325:90b] [ERROR]:Uncaught exception: -[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0 2014-09-15 22:00:19.243 SAPRetailEx[10325:90b] [ERROR]:-[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0 2014-09-15 22:00:19.248 SAPRetailEx[10325:90b] [ERROR]:(

  • You might want to try using the debugger to check what the object type of `diff` is after it's been set. It must be `NSMutableArray` for this to work. Also, you don't need to use `NSArray arrayWithArray:` when calling `removeObjectsInArray:`. You're just wasting CPU and RAM to create an array you don't need. – BergQuester Sep 15 '14 at 16:40
  • 1
    You can't mutate an immutable object. (This is what the exception message was clearly telling you.) – Hot Licks Sep 15 '14 at 16:42

2 Answers2

5

you cannot remove objects form NSArray; I know you think you are using an NSMutableArray but you are not. This is made quite clear from your error [__NSArrayI removeObjectsInArray:]: unrecognized selector; __NSArrayI is an immutable object (that is what the I means), if this were really a mutable array the class would be __NSArrayM.

you seem to be operating under the false impression that you can get a mutable array simply by casting an NSArray to NSMutableArray; this has never been how casting works under any C derived language. You instead need to send a mutableCopy message to the NSArray. Of course you now have a copy of the array so modifications arent reflected in the original. you also own the copy so if you arent using ARC you need to release the copy when you are done.

I'm curious to know why you are already making a mutableCopy of self.filteredProducts then also trying to remove directly from self.filteredProducts. even if this worked how you expect you are now copying the array for no reason then removing from both thee "original" and the "copy".

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • how can i convert self self.filteredProducts to NSMutableArray so that i can remove object from it which are same in diff array as i need it. – Nehit Gurubaxani Sep 15 '14 at 17:05
  • 1
    @NehitGurubaxani you need to declare self.filteredProducts as an `NSMutableArray` then where you are assigning an `NSArray` to it you instead do `self.filteredProducts` = [theArray mutableCopy]`. However, looking at your code that will be the same as `diff` so I dont know what you are trying to do. – Brad Allred Sep 15 '14 at 17:06
0

self.filteredProducts isn't really an NSMutableArray It's just a normal NSArray which doesn't have a "remove option".

[self.filteredProducts removeObjectsInArray:[NSArray arrayWithArray:diff]

To help in the future... set at break point in your code after you assign it, then print out the description and see what type of object it really is. either po in the debugger or right click on "Print Description". Don't believe what is says on the left panel, that's just what you told the code the object is, look at the description on the right to see what it really is.

enter image description here

DBD
  • 23,075
  • 12
  • 60
  • 84