views:

83

answers:

3

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.

+1  A: 

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

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

St3fan
Where is he treating it as an NSArray?
Chuck
A: 

From Apple's documentation,

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

so it cannot be used as a NSArray.

Rod
+2  A: 

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.

Chuck
Agreed; something like `initWithCGPDFArrayRef:` would probably be better.
Wevah
There was just a single warning, but you are correct nonetheless. I did rename the method and the warning disappeared. Thanks.
Sander van der Wal