我有继承类 B 的类 A。并希望实现如下所示。但是由于动态类型转换,init 方法会被递归调用。有没有办法做到这一点?有什么建议吗?(不改变子类中的'init's name?
@interface A : NSObject
@property NSData * data;
@end
@implementation A
- (id) init {
    self = [super init];
    /* want to do some default initialization here  
       for this class and all of its subclasses. */
    // ... ... 
    return self;
}
/* This [self init] get recursed. 
   But how can I force this class and all of its subclass to call [self init] above, 
   not to call the subclass's init method. */
- (id) initWithData:(NSData *)d {
    self = [self init];
    self.data = d;
    return self;
}
@end
@interface B : A
@end
#import "B.h"
@implementation B
- (id) init {
    self = [super initWithData:nil];
    // some subclass specific init code here ... 
    return 
}
@end
使用 B,
- (void) testInit{
    B * b = [[B alloc] init];
}