【问题标题】:Assiging base class pointer to derived class in objective c在目标c中将基类指针分配给派生类
【发布时间】:2010-07-27 04:02:26
【问题描述】:

我有一个名为 ModelBase 的基类,我有一个名为“Person”的派生类。 现在在人的 init 方法中,我有类似的东西

-(Person*) init {
     if(self = [super init])
         return self;

     return nil; 
}

然而,Objective C 抱怨说 初始化“struct ModelBase *”的不兼容 Objective-C 类型,预期为“struct Person *”。我只是用 [super init] 初始化 self ,它正在初始化指向派生类的基类指针。

我错过了什么?

【问题讨论】:

  • 你的标题是什么样的?

标签: iphone objective-c


【解决方案1】:

您是否声明了您的 ModelBase 初始化方法以返回 (ModelBase*) ?如果是这样,请将其更改为 return (id)。您会注意到 Cocoa 中的大多数初始化程序都出于这个原因返回 (id)。

代替

- (ModelBase*)init;

你想要的

- (id)init;

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多