我遇到了一些實現簡單的超/子類方案的麻煩。我在超類中聲明瞭一個NSMutableDictionary,並試圖在子類中訪問它,但它只返回null。任何幫助,將不勝感激。從子類訪問超類屬性
@interface RootModel : NSObject <Updatable, TouchDelegate>
@property (nonatomic) NSMutableDictionary *gameValues;
@end
@interface SubclassModel : RootModel
@end
@implementation RootModel
- (id)initWithController:(id)controller {
if ((self = [super init])) {
_controller = controller;
_gameValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:300.0f], KEY_JUMP_VELOCITY,
[NSNumber numberWithFloat:49.47f], KEY_GRAVITY,
[NSNumber numberWithFloat:0.25f], KEY_JUMP_TIME_LIMIT,
nil];
NSLog(@"%@", _gameValues);
}
return self;
}
@implementation SubclassModel
- (id)init{
if ((self = [super init])) {
// This NSLog prints "(null)" if using super.gameValues or self.gameValues, why?
NSLog(@"subclass: %@", super.gameValues);
}
return self;
}
@end
我在做什麼錯?
請參閱此[post](http://stackoverflow.com/questions/5588799/objective-c-how-do-you-access-parent-properties-from-subclass)descrption和detail,將刪除從您的代碼的第一行NSObject。 –