[英]Call method hierarchy in Objective-C
我声明一个类方法:
+(int)widthScaled:(UIImage *)image maximumHeight:(int)max{
int width = 0;
width = (image.size.width - (((image.size.height - max)*100)/image.size.height)*image.size.width)/100;
return width;
}
在同一类中,我声明了一个类似于以下的实例方法:
- (void)generarVistas{
if(...){
...
}else{
for(...){
...
//I need to put the class method here but if I call the class method like this, don't recognize
int variable = [[self class] widthScaled:image maximumHeight:max];
...
}
}
我尝试将类方法放在实例方法中,但不起作用。 我应该指出什么上下文来识别类方法?
您面临的问题是[self class]
是一个运行时调用,直到运行时才知道返回结果。 编译器不知道它是什么类。 您编写的代码必须始终从固定的类中调用,因此不要这样做。 如果该类是FooClass,则使用以下形式:
int variable = [FooClass widthScaled:image maximumHeight:max];
(将[self class]
替换为您的类名,例如FooClass
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.