swift中调用oc的类方法
在Swift中调用OC的类方法,需要先导入OC的头文件,然后通过类名直接调用类方法。以下是示例代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 调用OC的类方法
let result = Calculator.add(10, b: 20)
print("10 + 20 = \(result)")
}
}
其中,Calculator是OC中的一个类,包含一个add类方法:
@interface Calculator : NSObject
+ (NSInteger)add:(NSInteger)a b:(NSInteger)b;
@end
@implementation Calculator
+ (NSInteger)add:(NSInteger)a b:(NSInteger)b {
return a + b;
}
@end
在Swift中,可以直接通过类名调用OC的类方法Calculator.add(10, b: 20),并且可以使用返回值进行其他操作
原文地址: https://www.cveoy.top/t/topic/htyv 著作权归作者所有。请勿转载和采集!