初始化自定义类时出现问题(UIImageView 可能无法响应“-alloc”)
我对这一行有一个问题,
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:' 方法
谢谢
I have an issue for this line
ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game andCGRect:CGRectMake( 200, 100, 16, 16 )] ;
the issue is ' ball undeclared' with warning: 'UIImageView' may not respond to '-alloc' and warning: no '-initWithPNGFileName:andGame:andCGRect:' method found
Here's the method:
-(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 );
}
I don't understand why UIImageView can't be allocated and why I have no '-initWithPNGFileName:andGame:andCGRect:' method found
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您提供的代码来看,您似乎有一个名为
ball
的UIImageView
实例。您确定您的类名为ball
(顺便说一下,类名应以大写字母开头)并且它是.h
- 文件已正确包含吗?一些背景信息:
alloc
是一个类方法,您不会将其发送到实例 - 它的签名类似于+ (id) alloc;
(注意这里的加号!)基本上,警告表示您尝试在对象上调用实例方法 alloc,该方法将具有签名- (id) alloc;
(注意这里的减号!),如果它存在的话(很可能不是这种情况)。因此,编译器不会将ball
识别为类名,而是将其识别为UIImageView
的实例,正如警告所示,它可能不会响应- (id )分配;
.默认情况下,编译器假设未知方法返回
id
的实例(而不是您期望的ball
的实例),这就是为什么它警告您找不到该对象的名为-initWithPNGFileName:andGame:andCGRect
的方法。From the code you provide it seems that you have an instance of
UIImageView
calledball
. Are you sure your Class is calledball
(Class names should begin with a capital letter, by the way) and it's.h
- file is properly included?Some background information:
alloc
is a class method, you don't send it to an instance - it's signature is something like+ (id) alloc;
(mind the plus here!) Basically, the warning says that you try to invoke the instance method alloc on your object, which would have the signature- (id) alloc;
(mind the minus here!), if it existed (which is most probably not the case). Therefore, the compiler recognizesball
not as a class name, but as an instance ofUIImageView
, which - as the warning indicates - may not respond to- (id) alloc;
.By default, the compiler assumes that unknown methods return an instance of
id
(and not ofball
as you expected), this is why it warns you that it can't find a method called-initWithPNGFileName:andGame:andCGRect
for this object.