Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 初始化自定义类时出现问题(UIImageView可能不响应';-alloc';)_Objective C_Uiimageview_Alloc - Fatal编程技术网

Objective c 初始化自定义类时出现问题(UIImageView可能不响应';-alloc';)

Objective c 初始化自定义类时出现问题(UIImageView可能不响应';-alloc';),objective-c,uiimageview,alloc,Objective C,Uiimageview,Alloc,我对这一行有意见 ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game andCGRect:CGRectMake( 200, 100, 16, 16 )] ; 问题是“ball undeclared”,警告:“UIImageView”可能不响应“-alloc”,警告:no“-initWithPNGFileName:andGame:andCGRect:”方法已找到 方法如下: -(id) initWithPNG

我对这一行有意见

ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game andCGRect:CGRectMake( 200, 100, 16, 16 )] ;
问题是“ball undeclared”,警告:“UIImageView”可能不响应“-alloc”,警告:no“-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:'方法


感谢

从您提供的代码来看,您似乎有一个名为
ball
UIImageView
实例。您确定您的类名为
ball
(顺便说一句,类名应该以大写字母开头)并且它是
。h
-文件是否正确包括在内

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

.h包含得很好。UIImageView被命名为类这一事实产生了一个问题?我已经用更多的背景信息更新了我的答案。基本上,编译器不知道您正在处理UIImageView而不是类“ball”。重命名UIImageView应该可以解决您的问题。您可以在尝试alloc/init球的那行提供更多代码吗?我知道了,我更改了为对象提供图像的方法。谢谢你好。。你能把
ball
类的
.h
文件贴出来吗?