Unable to find out "warning: incompatible Objective-C types"

I have a subclass of NSObject that implements a method -(id)initWithRootElement:(MyElement *)e. NSXMLDocument has an identical method that accepts NSXMLElement. When I compile, I get the following warning:

warning: incompatible Objective-C types 'struct MyElement *', expected 'struct NSXMLElement *' when passing argument 1 of 'initWithRootElement:' from distinct Objective-C type

In this case, I compile Clang + LLVM on SnowLeopard with Xcode 3.2.1, but this also happens with GCC 4.2 on Leopard and SnowLeopard.

I don’t understand why this throws a warning for my direct subclass of NSObject when NSXMLDocument must first inherit NSXMLNode? Shouldn't he know that he -(id)initWithRootElement:(NSXMLElement *)eapplies only to NSXMLDocument, which has nothing to do with my class? I could understand if I was trying to overload this method, but I do not know. Please tell me I'm not losing my mind ...

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSXMLElement.h>
// Importing this here causes the warning...
// #import <Foundation/NSXMLDocument.h>

typedef NSObject MyElement;

@interface TestClass : NSObject
{
}

- (id)initWithRootElement:(MyElement *)element;
@end

@implementation TestClass
- (id)initWithRootElement:(MyElement *)element { return nil; }
@end

// ...but here it doesn't
// #import <Foundation/NSXMLDocument.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // No warning! Inheritance: NSXMLDocument -> NSXMLNode -> NSObject
    NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithName:@"foo"];
    [[TestClass alloc] initWithRootElement:xmlElement];

    // warning: incompatible Objective-C types 'struct MyElement *', expected 'struct NSXMLElement *' when passing argument 1 of 'initWithRootElement:' from distinct Objective-C type
    MyElement *element = [[MyElement alloc] init];
    [[TestClass alloc] initWithRootElement:element];

    [pool drain];
    return 0;
}
+3
1

Objective-C .

initWithRootElement: NSXMLDocument :

- (id)initWithRootElement:(NSXMLElement *)element;

:

- (id)initWithRootElement:(MyElement *)element;

, . ( element MyElement *...

[[TestClass alloc] initWithRootElement:element];

... +alloc id, , , , ; .

Objective-C ​​Apple , .

, . . , http://bugreport.apple.com/ # ( ).

+11

Source: https://habr.com/ru/post/1727187/


All Articles