简体   繁体   English

为什么此指针类型不兼容

[英]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 . 显然,编译器认为该数组与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: 如果我是对的,这就是您遇到的问题:

  1. There are two method signatures that go with that selector. 该选择器附带两个方法签名。 NSArray's takes an NSArray* and yours takes a CGPDFArrayRef . NSArray使用NSArray*而您使用CGPDFArrayRef

  2. alloc returns id . alloc返回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. 然后,您将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.

相关问题 指针类型不兼容?? 奇怪的 - Incompatible pointer type?? Strange 分配给&#39;ViewController&#39;的指针类型不兼容 - Incompatible pointer type assigning to 'ViewController' NSManagedObject不兼容的指针类型警告 - NSManagedObject Incompatible pointer type warnings 从不兼容的指针类型初始化 - initialization from incompatible pointer type XCODE不兼容的指针类型错误 - XCODE incompatible pointer type error 从不兼容的指针类型分配? - Assignment from incompatible pointer type? 使用UItableViewCell类型的表达式初始化&#39;CustomCellView *&#39;的不兼容指针类型 - Incompatible pointer type initializing 'CustomCellView *' with an expression of type UItableViewCell 不兼容的指针类型将“ UITapGestureRecognizer *”发送到“ UIView * _Nonnull”类型的参数 - Incompatible pointer types sending 'UITapGestureRecognizer *' to parameter of type 'UIView * _Nonnull' 不兼容的指针类型将“ NSString *”发送到类型为“ NSInteger *”的参数(又名“ int *”) - Incompatible pointer types sending 'NSString *' to parameter of type 'NSInteger *' (aka 'int *') 不兼容的指针类型将“ NSSortDescriptor * __ strong”发送到“ NSArray *”类型的参数 - Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM