iPhone - Crash using addObject on a NSMutableArray

前端 未结 5 1532
悲哀的现实
悲哀的现实 2020-12-10 19:42

I have a problem here. Or Maybe I\'m really tired...

I have a class :

@interface THECLASS : UIViewController  {
    NSMuta         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-10 20:21

    For some reason you're calling the addObject: method on an instance of NSArray, as the stack trace indicates. Perhaps the synthesized getter of your param object returns an NSArray? Don't use the getter in your method:

    - (void) changedSelectorValue:(NSIndexPath*)indexPath isOn:(BOOL)isOn {
        [param addObject:@"eeeee"];
    }
    

    Ultimately, the error is correct. You're calling a mutating method on an immutable object. Strange enough, in my testing this doesn't cause a compiler error or warning:

    NSMutableArray *array = [[NSArray alloc] init];
    

    Edit: Whether you want to believe it or not, somehow the param ivar is being set to an instance of an NSArray. Override the setter:

    - (void)setParam:(NSMutableArray *)p {
        if (param != p) {
            [param release];
            param = [p retain];
        }
    }
    

    Then add a breakpoint on that method, and check to see when an NSArray is being passed in.

提交回复
热议问题