Why are my objects changing unexpectedly?

后端 未结 3 1189
佛祖请我去吃肉
佛祖请我去吃肉 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:52

    This is not the root of yout problem but change your initWithJson: to

    -(id)initWithJson:(NSDictionary *)jsonObject
    {      
      if(!(self = [super init] ) ) { return nil; }    
      _references = jsonObject[@"references"];    
      return self;
    }
    

    Ditch the custom setter and getter (you obviously don't need them).

    In your case you can declare property simply with:

    @property (strong) NSArray* references;
    

    Change the NSArray * _references; to:

    @synthesize references = _references;
    

    You can also omit the @synthesize line in your case.

    Also change _references = jsonObject[@"references"]; to:

    _references = [NSArray arrayWithArray:jsonObject[@"references"]];
    

    To sum it all together:

    ANBNote.h

    @interface ANBNote : NSObject
    @property (strong) NSArray* references;
    - (id)initWithJson:(NSDictionary*)data;
    @end
    

    ANBNote.m

    #import "ANBNote.h"
    
    @implementation ANBNote
    
    -(id) init {
      if(!(self=[super init])) return nil;
      _references=@[];
      return self;
    }
    
    -(id)initWithJson:(NSDictionary *)jsonObject {      
      if(!(self = [super init] ) ) { return nil; }    
      _references = [NSArray arrayWithArray:jsonObject[@"references"]];    
      return self;
    }
    
    @end
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题