小编Ste*_*ine的帖子

Objective-C自定义属性getter无限递归

我有一个Objective-C类,它实现了数据树的节点.它的属性对公众是只读的,而类的私有扩展(这里未显示)实现了属性的setter,因此manager类可以创建树.

// Interface
@interface DataSet : NSObject {
    NSString        *name;
    NSString        *data;
@private
    DataSet         *parent;
    NSMutableArray  *children;
}
@property (nonatomic, readonly, copy) NSString *name;
@property (nonatomic, readonly, copy) NSString *data;
Run Code Online (Sandbox Code Playgroud)

我想为其中一个属性实现一个自定义getter,如果属性为nil,它将向上走,直到找到一个祖先节点,该节点具有该属性的非零值.

我的问题是实现getter而不会导致getter调用自身的无限递归.

// Implementation
@interface DataSet ()
@property (nonatomic, retain) DataSet           *parent;
@property (nonatomic, retain) NSMutableArray    *children;
@end

@implementation DataSet

@synthesize name;
// do not @synthesize data
@synthesize parent, children;

// custom getter walks up tree to find first non-nil 'data' property
- (NSString*) data {
    NSString *result = nil; …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ipad ios

2
推荐指数
1
解决办法
1890
查看次数

标签 统计

ios ×1

ipad ×1

iphone ×1

objective-c ×1