警告:“类型冲突...”创建 Objective-C (ios) 初始化程序时
我希望能够使用如下所示的内容来初始化我的一个类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能不是这样,但您的界面定义中似乎缺少一个分号:
应该是
,但也许这只是您编写问题时的复制和粘贴错误。
还要确保您
在 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:
should be
but maybe that is just a copy and paste error when you were writing up your question.
Also make sure you
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.您将返回
nil
。你必须应该总是返回一个对象。(感谢 Josh 指出你并不总是需要返回一个对象)像这样:另外,我刚刚记得遇到过与我自己类似的事情。确保在两个类(
ModelClass
)以及您在其中创建ModelClass
的控制器中导入TBXML.h
。问题是编译器不知道 TBXMLElement 是什么,因此您需要通过导入相关标头来指示它。You are returning
nil
. Youmustshould always return an object.(Thanks to Josh for pointing out that you don't always need to return an object) Like this:Also, I just remembered encountering something similar to that myself. Make sure you import
TBXML.h
in both classes, theModelClass
, as well as the controller where you are creating theModelClass
. The problem is that the compiler doesn't know what aTBXMLElement
is, so you need to instruct it by importing the relevant header(s).感谢您的帮助!
我在黑暗中摸索了一段时间,没有找到问题的原因,尽管“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.