初始化自定义类时出现问题(UIImageView 可能无法响应“-alloc”)

发布于 2024-10-18 06:45:17 字数 1345 浏览 1 评论 0原文

我对这一行有一个问题,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-10-25 06:45:17

从您提供的代码来看,您似乎有一个名为 ballUIImageView 实例。您确定您的类名为 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 called ball. Are you sure your Class is called ball (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 recognizes ball not as a class name, but as an instance of UIImageView, 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 of ball as you expected), this is why it warns you that it can't find a method called -initWithPNGFileName:andGame:andCGRect for this object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文