我有一个在初始化程序中有这个的类:
@implementation BaseFooClass
-(id) init
{
if (self = [super init])
{
// initialize instance variables that always need to start with this value
}
return self;
}
-(id) initWithSomeInt:(int) someInt
{
if (self = [self init]) // <-- I need to make sure that I am calling BaseFooClass's init here, not SubFooClass's, does that make sense?
{
self.someInt = someInt;
}
return self;
}
@end
这一切都很好,花花公子。我的问题是当我实现子类时:
@implementation SubFooClass
-(id) init
{
return [self initWithSomeInt:0];
}
-(id) initWithSomeInt:(int) someInt
{
if (self = [super init]) // <--- Infinite loop (stack overflow :) )
{
// initialize other variables
}
}
@end
我基本上需要专门调用BaseFooClass
'sinit
而不是SubFooClass
's init
。
我无法更改对象的初始化方式,因为我正在将项目从 C# 转换为在我的 iPad 应用程序中使用。
谢谢大家
编辑:
由于有人问,这是我的标题:
@interface BaseFooClass : NSObject
// implicit from NSObject
// -(id) init;
-(id) initWithSomeInt:(int) someInt;
// more methods
@end
@interface SubFooClass : BaseFooClass
// implicit from NSObject
// -(id) init;
// implicit from BaseFooClass
//-(id) initWithSomeInt:(int) someInt;
@end