-1

I have a Person class which has an instance variable: Name and Person.

@interface Person : NSObject{
    NSString *name;
    Person *child;
}

@property (copy) NSString *name;
@property (strong) Person *child;
@end

@implementation Person
@synthesize name;
@synthesize child;

-(id) init{
    self = [super init];
    if(self){
        name = @"";
        child = [[Person alloc]init];
    }
    return self;
}
@end

If I create a Person object, like:

Person *parent = [[Person alloc]init]; 

the program will repeat to create Person instances, how can I solve this?

4

1 回答 1

3

不是所有人都有孩子,那么为什么每次创建一个新人时都要分配一个孩子呢?

换句话说,不要做你正在做的事情。如果一个人碰巧有一个孩子,请单独创建该孩子并child适当地设置该人的属性。

于 2012-07-10T14:27:16.160 回答