gpt4 book ai didi

objective-c - 从实例方法调用类方法

转载 作者:行者123 更新时间:2023-12-02 21:50:11 26 4
gpt4 key购买 nike

我想从实例方法调用类方法以使用其返回值。

这是我的类方法

+(double) minFMFrequency {

return 88.3;
}

这是我的实例方法

-(void) chackFrequency {

switch (band) {
case 'F':
if (self.frequency > Value obtained from class method )
frequency=107.9;
break;

default:
break;
}

}

bandfrequency是实例变量。

最佳答案

+(void)classMethod
{
[self anotherClassMethod]; // in a class method, self refers to the class
}

-(void)instanceMethod
{
[self anotherInstanceMethod]; //in an instance method self refers to the object, or instance

[[self class] classMethod]; //to call a class method from an instance send the instance the class message

}

所以在你的情况下:[[self class] minFMFrequency];

关于objective-c - 从实例方法调用类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com