NSMutableArray addObject: -[__NSArrayI addObject:]: unrecognized selector sent to instance

后端 未结 9 1066
太阳男子
太阳男子 2020-11-30 21:28

I have tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING is working for me. I tried setting it equal to a newly allocated and initialized NSMutableArra

9条回答
  •  我在风中等你
    2020-11-30 21:47

    As I was proof reading my post, a thought occurred to me and I answered my own question. This resolution was obscure enough that I decided to go ahead, create the post and answer it myself (so any other newbies, like myself, won't get hung up).

    My mistake was in...

    @property (copy) NSMutableArray *array;
    

    it should have been...

    @property (retain) NSMutableArray *array;
    

    The error was not happening in the way I was executing my code, but rather in the way the anObject was attempting to "copy" the NSMutableArray array.

    As we all know...

    mutableArray = [mutableArray copy];
    

    is not always (or ever, in my experience) equal to...

    mutableArray = [mutableArray mutableCopy];
    

    And this was the source of my problem. By simply switching the @property from (copy) to (retain) I solved my problem.

提交回复
热议问题