简体   繁体   中英

Why is this pointer type incompatible

This is the code

Dest.h

#import <UIKit/UIKit.h>

#import <CoreGraphics/CGPDFArray.h>

@class Model;

// snip
@interface Dest : NSObject 
{
    CGPDFArrayRef destArray;

    DestKind kind;
}

+ (id)destWithObject:(CGPDFObjectRef)obj inModel:(Model*)model;

- (id)initWithArray:(CGPDFArrayRef)array;

Dest.m

@implementation Dest

+ (id)destWithObject:(CGPDFObjectRef)obj inModel:(PDFModel*)model
{
    CGPDFArrayRef array = NULL;
    Dest* dest = nil;

    // stuff to create array

    if (array)
    {
        dest = [[[Dest alloc] initWithArray:array] autorelease];  

<path>/Dest.m:63: warning: passing argument 1 of 'initWithArray:' from incompatible pointer type

    }

    return dest;
}

Clearly the compiler thinks that array is incompatible with initWithArray: declared in Dest.h . But as far as I can see, the type is exactly right. I even copied the declaration from Dest.h and pasted it in Dest.m . initWithArray: compiles fine. Adding/removing the CGPDFArray.h header file in Dest.h doesn't make any difference, the compiler doesn't think it is an int in Dest.h .

I have a feeling you're leaving out another warning that's relevant — "warning: multiple methods named 'initWithArray:' found". If I'm right, this is what you're running into:

  1. There are two method signatures that go with that selector. NSArray's takes an NSArray* and yours takes a CGPDFArrayRef .

  2. alloc returns id . This means that the compiler has no idea what class it returns (yes, the compiler is that thick).

  3. You then send initWithArray: to this mystery object. The compiler says, "Gosh, I don't know what kind of object this is, so I can't decide which method signature is correct. I'll spin around really fast and whichever one I'm facing is the one I'll pick." It chooses NSArray's signature. Then it looks at the argument and says, "Hey, that's not an NSArray! Error!"

The quick-and-easy solution is to change it to [[(Dest*)[Dest alloc] initWithArray:array] autorelease] . The better solution is to choose a distinct selector for your method.

Oh don't do that. Only CFArrayRef s are 'toll-free bridged' to NSArray . The CGPDFArrayRef however is completely different and incompatible. You can not use those as NSArray s.

The PDF API sure looks like a standard Core Foundation compatible one, but it really is not.

From Apple's documentation ,

CGPDFArray header file defines an opaque type that encapsulates a PDF array

so it cannot be used as a NSArray .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM