我有一個關於變量範圍和子類的問題。瞭解子/父實例變量的範圍
我有類似如下:
ParentClass.h
@interface ParentClass : NSObject
@property (nonatomic, strong, readonly) NSString *myIdentifier;
- (id)initWithIdentifier:(NSString *)identifier;
@end
ParentClass.m
@interface ParentClass()
@property (nonatomic, strong) NSString *myIdentifier;
@end
@implementation ParentClass
- (id)initWithIdentifier:(NSString *)identifier {
if (self = [super init]) {
self.myIdentifier = identifier;
}
return self;
}
@end
ChildClass.h
@interface ChildClass : ParentClass
- (NSString *)showMeTheIdentifier;
@end
ChildClass.m
@implementation ChildClass
- (NSString *)showMeTheIdentifier {
return self.myIdentifier;
}
然後我去實現類和我沒有得到任何的編譯器警告,但NSLog的結果是(null)
ViewController.m
...
ChildClass *c = [[ChildClass alloc] initWithIdentifier:@"abc123"];
NSLog(@"What's my identifier? %@", [c showMeTheIdentifier]);
...
我在做什麼錯?
感謝
你是否在你的'ParentClass'初始化程序中缺少'self = [super init]'東西,或者爲了簡潔起見,你在問題中忽略了它? – veddermatic
'@property(nonatomic,strong)NSString * myIdentifier;'to .h –
'NSLog(@「Just c:'%@'」,c);'output? – Wain