2

我有一个非常基本的示例,我正在从 JSON 将一些数据读入一个类,并且我的对象正在以某种方式损坏。我怀疑我遗漏了一些关于属性/ARC 是如何工作的细节,但我不确定它是什么,或者我将如何追踪它。

我已经减少了我的原始代码,所以问题很明显。然而,这意味着不清楚我为什么要使用自定义属性等 - 我的真实代码在那里有更多功能......

该问题可以在 Test.m 文件的最后一行中看到。第一个构造的对象现在包含来自第二个对象的数据,而不是它最初包含的值。

任何关于我做错了什么和/或如何追踪此类问题的建议将不胜感激。

ANBNote.h

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

ANBNote.m

#import "ANBNote.h"

@implementation ANBNote
NSArray * _references;

-(id) init {
  if(!(self=[super init])) return nil;
  _references=@[];
  return self;
}

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

-(void) setReferences:(NSArray *)references {
  _references = references;
}

-(NSArray *)references {
  return _references;
}    

@end

测试.m

...
NSDictionary * d1 = @{@"references":@[@"r1",@"r2"]};
NSDictionary * d2 = @{@"references":@[@"q1",@"q2"]};

ANBNote * n1 = [[ANBNote alloc] initWithJson:d1];
NSLog(@"R1 = %p, %@", n1.references, n1.references); // Prints r1, r2 - as expected

ANBNote * n2 = [[ANBNote alloc] initWithJson:d2];
NSLog(@"R2 = %p, %@", n2.references, n2.references); // Prints q1, q2 - as expected
NSLog(@"R1 = %p, %@", n1.references, n1.references); // Prints q1, q2 - Oh No!

请注意,如果我删除自定义引用属性函数并依赖编译器生成的版本,一切似乎都正常。

4

3 回答 3

2

这不是 ivar:

@implementation ANBNote
NSArray * _references;

这是一个全球性的。您的类的所有实例只有一个,而不是每个实例一个。当下一个实例设置它时,前面的实例会看到新值,因为它是同一个变量。您需要将其放入花括号中以使其成为 ivar:

@implementation ANBNote
{
    NSArray * _references;
}

但是,无需显式声明变量——您仍然可以自己实现访问器,并让编译器创建 ivar,只要您使用默认的合成名称(下划线 + 属性名称)。

于 2013-08-17T06:25:46.697 回答
2

答案很简单,您将 NSArray *_references 定义为静态变量而不是私有变量。要做到这一点

@implementation ANBNote{
    NSArray * _references;
}

除此之外,从 Xcode 4 开始,您不必在实现文件中定义 _references 对象。您可以在头文件中设置一个变量,然后通过_在其名称前键入来访问其私有访问器。

例如

@interface ANBNote
@property(strong , nonatomic) NSArray *references;
@end

@implementation ANBNote

-(id) initWithArray:(NSArray *) ar{
    if(self)
        _references = [NSArray arrayWithArray:ar];
    return self;
}

@end
于 2013-08-17T06:31:46.393 回答
0

这不是你问题的根源,而是改变你initWithJson:

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

抛弃自定义设置器和获取器(您显然不需要它们)。

在您的情况下,您可以简单地声明属性:

@property (strong) NSArray* references;

更改NSArray * _references;为:

@synthesize references = _references;

您也可以@synthesize在您的情况下省略该行。

_references = jsonObject[@"references"];改为:

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

总结一下:

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
于 2013-08-17T09:21:02.120 回答