简体   繁体   中英

Calling Other Classes' Instance Method from Class Method? (Objective C)

I have the following method in a class named "Item". As you can see, getImage is a class/static method, but I want it to return an instance method from a DIFFERENT class (Item Instance). I don't see why it is not working?

+(UIImage*)getImage:(int)itemNumber {
    UIImage *image = [ItemInstance getImage:itemNumber];
    return image;
}

It tells me that the class method +getImage cannot be found in ItemInstance, but I am trying to call the instance method -getImage, not the class method +getImage

To call an instance method, you have to have an instance:

+ (UIImage*)getImage:(int)itemNumber {
    ItemInstance *anInstance = [[ItemInstance alloc] init];

    UIImage *image = [anInstance getImage:itemNumber];

    return image;
}

Use below code:

+(UIImage*)getImage:(int)itemNumber {
     ItemInstance *obj = [[[ItemInstance alloc]init] autorelease];
     UIImage *image = [obj getImage:itemNumber];
     return image;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM