开发者

Warning: 'Conflicting types...' when creating Objective-C (ios) initializer

开发者 https://www.devze.com 2023-03-06 07:57 出处:网络
I want to be able to initialize one of my classes with something that looks like this ModelClass *aModelClass = [[ModelClass alloc] initWithXML:imageXML];

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 i开发者_如何学Cmplementation 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.


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号