不为子类调用的基类代表

时间:2015-10-09 12:08:18

标签: ios objective-c cocoa-touch

我正在开发一个IOS应用程序。我创建了一个基类来实例化具有委托的服务器类。我在init方法中设置了该委托。我期待代理将为所有子类设置,但由于某种原因尚未发生。代码如下。 BaseClass.h

string1

BaseClass.m

@interface BBBaseCommand : NSObject<SectionCommand,ServerDelegate>
{
    Server *serverObj;
}
- (instancetype)init;
- (void)serverDidReceiveResponse:(NSDictionary *)response;
- (void)serverDidReceiveError:(NSError *)error;
@end

SubClass.h

-(instancetype)init
{
    serverObj = [[Server alloc]init];
    [serverObj setDelegate:self];
    return self;
}

SubClass.m

@interface BBBoatSaleCommand : BBBaseCommand
-(void)excute;
-(instancetype)init;
@end

提前感谢。

1 个答案:

答案 0 :(得分:1)

-(instancetype)init
{
    serverObj = [[Server alloc]init];
    [serverObj setDelegate:self];
    return self;
}

你永远不会在这里创建自我对象。

应该是

-(instancetype)init
{
    if(self = [super init]){
        serverObj = [[Server alloc]init];
        [serverObj setDelegate:self];
    }
    return self;
}