我以为这是100%直截了当的,现在我感觉不胜枚举。我有一个带有 public 属性的基于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的初始化是通过调用setup方法的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和子类中调用了setup方法。

感觉上这肯定是一个相当基本的问题,但是我无法解决这个问题。任何帮助将不胜感激。干杯!

解决方案:如下所述。令人尴尬,但仍然开心。首先到达Putz1103实在是很荣幸。我认为 super 的设置将通过其初始化方法调用,但并不是很明显...

最佳答案

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

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

您是否也要调用 super 设备的设置?
- (void)setup{
    [super setup];
    self.minimumAcceptedValuePerRound = @[ @5, @5, @5, @5, @5 ];
    self.scoreGoal = 25;
    self.acceptedValueAdditionWhenScoreGoalReached = 0;
}

10-08 12:11