警告:“类型冲突...”创建 Objective-C (ios) 初始化程序时

发布于 2024-11-07 08:17:02 字数 855 浏览 4 评论 0原文

我希望能够使用如下所示的内容来初始化我的一个类

ModelClass *aModelClass = [[ModelClass alloc] initWithXML:imageXML];

,这就是我在 interface 文件中编写的内容:

-(id)initWithXML:(TBXMLElement *)imageXML

以及在 implementation 文件中编写的内容:

-(id)initWithXML:(TBXMLElement *)imageXML
{
    self = [super init];

    if(imageXML) 
    {
        // do stuff with self.foo
        return self;
    }
    return nil;
}

所以我在接口文件和实现文件中都声明了它。但我在两者上遇到的错误是它们是冲突的。那么,如果不在两者中声明完全相同的方法签名,我该怎么做呢?

我现在得到的错误是:

冲突的类型 '-(id)initWithXML:(TBXMLElement *)imageXML'

我主要查看这篇文章来了解如何这样做并理解应该如何在 Objective-C 中完成,但这没有任何线索可以帮助我解决我的问题。

I want to be able to initialize one of my classes with something that looks like this

ModelClass *aModelClass = [[ModelClass alloc] initWithXML:imageXML];

So this is what I wrote in the interface file:

-(id)initWithXML:(TBXMLElement *)imageXML

and like this in the implementation file:

-(id)initWithXML:(TBXMLElement *)imageXML
{
    self = [super init];

    if(imageXML) 
    {
        // do stuff with self.foo
        return self;
    }
    return nil;
}

So I have declared it in the interface file and also the implementation file. But the error I get on both is that they are conflicting. So how do I do this if not declare the exact same method signature in both?

The error I now get is:

Conflicting types for
'-(id)initWithXML:(TBXMLElement
*)imageXML'

I was looking mostly at this article to understand how to do this and understanding how it should be done in Objective-C but this has not clues as to help me with my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南薇 2024-11-14 08:17:02

可能不是这样,但您的界面定义中似乎缺少一个分号:

-(id)initWithXML:(TBXMLElement *)imageXML

应该是

-(id)initWithXML:(TBXMLElement *)imageXML;

,但也许这只是您编写问题时的复制和粘贴错误。

还要确保您

#import "TBXML.h" 

在 ModelClass.h 中(如果您将其包含在 ModelClass.h 中,则导入 ModelClass.h 的其他 .m 文件也将获得它们需要的 TBXMLElement 的定义。

This may not be it, but you do appear to missing a semi-colon in your interface definition:

-(id)initWithXML:(TBXMLElement *)imageXML

should be

-(id)initWithXML:(TBXMLElement *)imageXML;

but maybe that is just a copy and paste error when you were writing up your question.

Also make sure you

#import "TBXML.h" 

in ModelClass.h (if you include it in ModelClass.h then other .m files that import ModelClass.h will also get the definitions for TBXMLElement which they will need.

暖阳 2024-11-14 08:17:02

您将返回nil。你必须应该总是返回一个对象。(感谢 Josh 指出你并不总是需要返回一个对象)像这样:

-(id)initWithXML:(TBXMLElement *)imageXML {
    if (self = [super init]) {
        if(imageXML) {
            // do stuff with self.foo
        }
    }
    return self;
}

另外,我刚刚记得遇到过与我自己类似的事情。确保在两个类(ModelClass)以及您在其中创建ModelClass 的控制器中导入TBXML.h。问题是编译器不知道 TBXMLElement 是什么,因此您需要通过导入相关标头来指示它。

You are returning nil. You must should always return an object.(Thanks to Josh for pointing out that you don't always need to return an object) Like this:

-(id)initWithXML:(TBXMLElement *)imageXML {
    if (self = [super init]) {
        if(imageXML) {
            // do stuff with self.foo
        }
    }
    return self;
}

Also, I just remembered encountering something similar to that myself. Make sure you import TBXML.h in both classes, the ModelClass, as well as the controller where you are creating the ModelClass. The problem is that the compiler doesn't know what a TBXMLElement is, so you need to instruct it by importing the relevant header(s).

绝不放开 2024-11-14 08:17:02

感谢您的帮助!
我在黑暗中摸索了一段时间,没有找到问题的原因,尽管“sudo rm -rf”和“idz”在这里发布的两个解决方案似乎都有可能。

我简单地删除了代码并从头开始重新编写后,我就开始工作了。

Thanks for the help!
I fondled in the dark for while and didn't find the cause of the problem although both solutions posted here by "sudo rm -rf" and "idz" seem likely.

I worked after I simple deleted my code and wrote it again from the beginning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文