I want to remove objects from NSmutableArray can one tell me the Best way to remove from NSMutableArray
.h
@property(nonatomic,retain)NSMutableArray *arr_property;
.m
_arr_property=[[NSMutableArray alloc]init];
MTPop *lplv = [[MTPop alloc] initWithTitle:SelectProperty(APP_SHARE.language)
options:[_arr_property valueForKeyPath:@"property_list.property_type_name"]
handler:^(NSInteger anIndex) {
txt_Property.text=[[_arr_property valueForKeyPath:@"property_list.property_type_name"] objectAtIndex:anIndex];
NSLog(@"index number %ld",(long)anIndex);
remove object--->>>
NSLog(@"index number %@",[_arr_property valueForKey:@"property_list"]);
[[_arr_property valueForKeyPath:@"property_list.property_type_name"] removeObjectAtIndex:anIndex]; ////hear the app is crashing
app is crashing error iam getting is
2015-06-09 13:21:31.104 Estater[2170:62264] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
Think about your code:
You now have an empty NSMutableArray. It has no elements.
What did we just say? The array has no elements. It has no element 0 - to have an element 0 it would need to have one element at least, but it doesn't. There is nothing to remove.
That part is the weirdest, but let's carry on. When called on an array,
valueForKeyPath:
results in an NSArray - not an NSMutableArray. So this gives you an empty NSArray. But you cannot sayremoveObjectAtIndex:
to an NSArray, even if it empty - it is not mutable. That's the crash you are experiencing.