problem initializing a custom class (UIImageView may not respond to '-alloc')

1.2k views Asked by At

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

1

There are 1 answers

4
Philipp On BEST ANSWER

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.