1

我对这条线有疑问

ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game andCGRect:CGRectMake( 200, 100, 16, 16 )] ;

问题是带有警告的“未声明的球”:“UIImageView”可能无法响应“-alloc”和警告:未找到“-initWithPNGFileName:andGame:andCGRect:”方法

这是方法:

-(id) initWithPNGFileName:(NSString *) filename andGame: (Game*) game andCGRect: (CGRect) imagerect_ {  
    [super init];

    CGDataProviderRef provider;
    CFStringRef path;
    CFURLRef url;


    const char *filenameAsChar = [filename cStringUsingEncoding:[NSString defaultCStringEncoding]]; //CFStringCreateWithCString n'accepte pas de NSString

    path = CFStringCreateWithCString (NULL, filenameAsChar, kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    provider = CGDataProviderCreateWithURL (url);
    CFRelease (url);
    image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);

    CGDataProviderRelease (provider);

    imageRect = imagerect_ ;

    [game addToDraw: self] ;
    [game addToTimer : self] ;

    return self ;

}


-(void) draw: (CGContextRef) gc
{
    CGContextDrawImage (gc, imageRect, image );
}

我不明白为什么无法分配 UIImageView 以及为什么我没有找到 '-initWithPNGFileName:andGame:andCGRect:' 方法

谢谢

4

1 回答 1

2

从您提供的代码看来,您有一个UIImageView被调用的实例ball。你确定你的类被调用ball(顺便说一下,类名应该以大写字母开头)并且它的.h- 文件被正确包含了吗?

一些背景信息:alloc是一个类方法,你不要将它发送到实例 - 它的签名类似于+ (id) alloc;(注意这里的加号!)基本上,警告说你试图在你的对象上调用实例方法alloc,它会有签名- (id) alloc;(注意这里的减号!),如果它存在(很可能不是这种情况)。因此,编译器ball不会将其识别为类名,而是将其识别为 的实例UIImageView,如警告所示,它可能无法响应- (id) alloc;。默认情况下,编译器假定未知方法返回一个实例id(而不是ball你期望的),这就是为什么它警告你它找不到-initWithPNGFileName:andGame:andCGRect为此对象调用的方法的原因。

于 2011-02-16T21:15:58.847 回答