[英]Why is this pointer type incompatible
This is the code 这是代码
#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;
@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
. 显然,编译器认为该数组与Dest.h
声明的initWithArray:
不兼容。 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
. 我什至从Dest.h
复制了声明,并将其粘贴到Dest.m
。 initWithArray:
compiles fine. initWithArray:
编译正常。 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
. 添加/删除的CGPDFArray.h
在头文件Dest.h
没有任何区别,编译器不会认为它是一个int Dest.h
。
I have a feeling you're leaving out another warning that's relevant — "warning: multiple methods named 'initWithArray:' found". 我感觉到您遗漏了另一个相关的警告-“警告:发现了多个名为'initWithArray:'的方法”。 If I'm right, this is what you're running into: 如果我是对的,这就是您遇到的问题:
There are two method signatures that go with that selector. 该选择器附带两个方法签名。 NSArray's takes an NSArray*
and yours takes a CGPDFArrayRef
. NSArray使用NSArray*
而您使用CGPDFArrayRef
。
alloc
returns id
. alloc
返回id
。 This means that the compiler has no idea what class it returns (yes, the compiler is that thick). 这意味着编译器不知道它将返回什么类(是的,编译器那么厚)。
You then send initWithArray:
to this mystery object. 然后,您将initWithArray:
发送到此神秘对象。 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. 它选择NSArray的签名。 Then it looks at the argument and says, "Hey, that's not an NSArray! Error!" 然后,它查看参数并说:“嘿,那不是NSArray!错误!”
The quick-and-easy solution is to change it to [[(Dest*)[Dest alloc] initWithArray:array] autorelease]
. 快速简便的解决方案是将其更改为[[(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
. 只有CFArrayRef
被“免费桥接”到NSArray
。 The CGPDFArrayRef
however is completely different and incompatible. 但是, CGPDFArrayRef
完全不同CGPDFArrayRef
兼容。 You can not use those as NSArray
s. 您不能将它们用作NSArray
。
The PDF API sure looks like a standard Core Foundation compatible one, but it really is not. PDF API肯定看起来像是与Core Foundation兼容的标准,但实际上不是。
From Apple's documentation , 根据Apple的文档 ,
CGPDFArray header file defines an opaque type that encapsulates a PDF array CGPDFArray头文件定义了封装PDF数组的不透明类型
so it cannot be used as a NSArray
. 因此它不能用作NSArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.