Why are my objects changing unexpectedly?

后端 未结 3 1212
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 11:45

I\'ve got a very basic example where I\'m reading some data into a class from JSON, and my objects are getting corrupted somehow. I suspect I\'m missing some detail about how pr

3条回答
  •  Happy的楠姐
    2021-01-22 12:16

    This is not a ivar:

    @implementation ANBNote
    NSArray * _references;
    

    it's a global. There's only one for all the instances of your class, not one for each. When the next instance sets it, the previous instances see the new values because it's the same variable. You need to put it into curly braces to make it an ivar:

    @implementation ANBNote
    {
        NSArray * _references;
    }
    

    There's no need to declare the variable explicitly, though -- you can still implement accessors yourself and let the compiler create the ivar as long as you use the default synthesized name (underscore + property name).

提交回复
热议问题