Luda
Luda

Reputation: 7068

Crash when removing an object from NSMutableArray

I have a NSMutableArray that I've initialised in viewDidLoad:

 self.titlesTagArreys = [@[@"Dollar", @"Euro", @"Pound",@"Dollar longString", @"Euro longStringlongString", @"Pound",@"Dollar", @"Euro", @"PoundlongStringlongString"]mutableCopy];

in .h:

 @property(nonatomic, copy) NSMutableArray* titlesTagArreys;

When I try to delete one item, the app crashes:

-(void)removeButtonWasPressed:(NSString*)tagTitle{
    NSLog(@"tagTitle - %@",tagTitle);
    NSLog(@"self.titlesTagArreys - %@",self.titlesTagArreys);    
    [self.titlesTagArreys removeObject:tagTitle];
}

Here is the log:

2013-08-06 16:15:03.989 EpicTv[6378:907] tagTitle - Dollar
2013-08-06 16:15:03.991 EpicTv[6378:907] self.titlesTagArreys - (
    Dollar,
    Euro,
    Pound,
    "Dollar longString",
    "Euro longStringlongString",
    Pound,
    Dollar,
    Euro,
    PoundlongStringlongString
)
[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0'
*** First throw call stack:
(0x327162a3 0x3a5c197f 0x32719e07 0x32718531 0x3266ff68 0x20ad55 0x20c9a5 0x20bf5d 0x346090c5 0x34609077 0x34609055 0x3460890b 0x34608e01 0x345315f1 0x3451e801 0x3451e11b 0x362295a3 0x362291d3 0x326eb173 0x326eb117 0x326e9f99 0x3265cebd 0x3265cd49 0x362282eb 0x34572301 0xafb89 0xa4d68)
libc++abi.dylib: terminate called throwing an exception

Upvotes: 2

Views: 2688

Answers (4)

griz
griz

Reputation: 470

It seems that titlesTagArrays list not an NSMutableArray because removeObject can not be called. Maybe you passed an NSArray earlier in the code to titlesTagArreys.

try to init your Array with

self.titlesTagArreys = [NSMutableArray arrayWithArray:@[@"...",@"...",...]];

@property(nonatomic, copy) makes a not mutable copy of you NSMutableArray. try @property(nonatomic, retain) instead of copy

Upvotes: 6

Manoj Udupa
Manoj Udupa

Reputation: 60

titlesTagArray is not NSMutableArray. This is because , in the logs we can see [__NSArrayI removeObject:] unrecognized selector sent to instance 0x1c53bbd0. Also NSArrayI is used for NSArray and NSArrayM is used for NSMutableArray. You must have initialised the mutablearray with an NSArray hence the exception.

Upvotes: 0

johnnieb
johnnieb

Reputation: 4522

I had the same problem and learned that you must override the setter for the mutable array property and call mutableCopy. You'll find the answer in Stack Overflow here.

Upvotes: 0

B.S.
B.S.

Reputation: 21726

I also think that you titlesTagArreys is not mutable array because of some code changes

Try to add: NSLog(@"%@", NSStringFromClass(self.titlesTagArreys.class)); to check what class do you use

-(void)removeButtonWasPressed:(NSString*)tagTitle{

  NSLog(@"%@", NSStringFromClass(self.titlesTagArreys.class));

  [self.titlesTagArreys removeObject:tagTitle];
}

Upvotes: 5

Related Questions