为什么此指针类型不兼容 [英] Why is this pointer type incompatible

查看:73
本文介绍了为什么此指针类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

#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;
}

很显然,编译器认为数组与 Dest.h 中声明的 initWithArray:不兼容.但是据我所知,这种类型是完全正确的.我什至从 Dest.h 复制了声明,并将其粘贴到 Dest.m 中. initWithArray:可以正常编译.在 Dest.h 中添加/删除 CGPDFArray.h 头文件没有任何区别,编译器认为它不是 Dest中的int.h .

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.

推荐答案

我觉得您正在忽略另一个与之相关的警告-警告:发现了多个名为'initWithArray:'的方法".如果我是对的,这就是您遇到的问题:

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. 该选择器附带两个方法签名.NSArray使用 NSArray * ,而您使用 CGPDFArrayRef .

alloc 返回 id .这意味着编译器不知道它将返回什么类(是的,编译器那么厚).

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

然后将 initWithArray:发送到此神秘对象.编译器说:天哪,我不知道这是哪种对象,所以我无法确定哪种方法签名是正确的.我会快速旋转,无论我面对的是我自己的那个.我会选择."它选择NSArray的签名.然后,它查看参数,并说:嘿,那不是NSArray!错误!"

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!"

快速简便的解决方案是将其更改为 [[((Dest *)[Dest alloc] initWithArray:array] autorelease] ).更好的解决方案是为您的方法选择一个不同的选择器.

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.

这篇关于为什么此指针类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆