2014-01-27 136 views
1

我认为这是100%直截了当,我感觉不止有点愚蠢创立现在。我有一个NSObject基于类NORPlayer有公共财产:从父母类继承财产

@property (nonatomic, strong) NSArray *pointRollers;

但是,这是不是由子类继承。 阵列设置这样的,它工作得很好:

父类:

@implementation NORPlayer 

- (instancetype)init{ 
    self = [super init]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 


- (void)setup{ 
    NSMutableArray *tempRollersArray = [[NSMutableArray alloc] init]; 
    for (NSUInteger counter = 0; counter < 5; counter++) { 
     NORPointRoller *aRoller = [[NORPointRoller alloc] init]; 
     [tempRollersArray addObject:aRoller]; 
    } 
    _pointRollers = [NSArray arrayWithArray:tempRollersArray]; 
} 

当试图创建一个子类从NORPlayerNORVirtualPlayer然而事情就会出差错:

SUB-CLASS:

#import "NORPlayer.h" 

@interface NORVirtualPlayer : NORPlayer 

// none of the below properties nor the method pertains to the problem at hand 
@property (nonatomic, assign) NSArray *minimumAcceptedValuePerRound; 
@property (nonatomic, assign) NSUInteger scoreGoal; 
@property (nonatomic, assign) NSUInteger acceptedValueAdditionWhenScoreGoalReached; 

- (void)performMoves; 

@end 

NORVirtualPlayer的初始化镜像其父级与init方法调用设置方法:

@implementation NORVirtualPlayer 

- (instancetype)init{ 
    self = [super init]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 


- (void)setup{ 
    self.minimumAcceptedValuePerRound = @[ @5, @5, @5, @5, @5 ]; 
    self.scoreGoal = 25; 
    self.acceptedValueAdditionWhenScoreGoalReached = 0; 
} 

的问题是,NORVirtualPlayer情况下,从来没有得到一个开始pointRollers财产。我已经通过了一切,并且parentClass中的设置方法与子类一样被调用...

这感觉就像它一定是一个相当基本的问题,但我只是无法围绕它来包围我的头。任何帮助将不胜感激。干杯!


解决方案:如下所述。虽然令人尴尬但很高兴。对Putz1103的荣誉实际上首先到达那里。我想超级的设置将被称为它的初始化方法,但不是那么明显......

+1

你的子类'setup'函数被调用了两次吗?它可能超载了父类的'setup'函数,所以实际上不再存在。你可以在你的sublcass设置功能中调用'[super setup]'。 – Putz1103

+0

@MrBr Yup,'self.pointRollers = [tempRollersArray copy]'和_pointRollers = [tempRollersArray copy]'都无济于事。 –

+0

@ Putz1103不,我已经完成了一切。子类'init执行运行父母设置的父母init。然后它运行子类的安装后... –

回答

4

我没有看到你的NORPlayer的设置从NORVirtualPlayer调用,这是数组初始化的地方。

- (void)setup{ 
    self.minimumAcceptedValuePerRound = @[ @5, @5, @5, @5, @5 ]; 
    self.scoreGoal = 25; 
    self.acceptedValueAdditionWhenScoreGoalReached = 0; 
} 

你想打电话给你的超级设置吗?

- (void)setup{ 
    [super setup]; 
    self.minimumAcceptedValuePerRound = @[ @5, @5, @5, @5, @5 ]; 
    self.scoreGoal = 25; 
    self.acceptedValueAdditionWhenScoreGoalReached = 0; 
}