2014-01-27 172 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; 
}