Why are my objects changing unexpectedly?

后端 未结 3 1188
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  臣服心动
    2021-01-22 11:58

    The answer is simple, you defined NSArray *_references as a static variable not as a private variable. To do that

    @implementation ANBNote{
        NSArray * _references;
    }
    

    In addition to this as of Xcode 4 you do not have to define _references object in the implementation file. You may just set a variable in the header file and then access its private accessor by just typing _ before the name of it.

    For instance

    @interface ANBNote
    @property(strong , nonatomic) NSArray *references;
    @end
    
    @implementation ANBNote
    
    -(id) initWithArray:(NSArray *) ar{
        if(self)
            _references = [NSArray arrayWithArray:ar];
        return self;
    }
    
    @end
    

提交回复
热议问题