【发布时间】:2021-01-08 15:15:37
【问题描述】:
我有一个类使用可以从 objc-c 和 swift 访问的类计算变量。我想测试所有以“const”开头的属性。
我有这个:
import UIKit
class MyClass: NSObject {
@objc class var constMethod1 : UIColor {
print("Method1")
return UIColor.red
}
@objc class var constMethod2 : UIColor {
print("Method2")
return UIColor.green
}
}
var methodCount: UInt32 = 0
let methodList = class_copyMethodList(MyClass.self, &methodCount)
for i in 0..<Int(methodCount){
let unwrapped = methodList?[i]
// call method only if it starts with "const"
let crtMethodStr = NSStringFromSelector(method_getName(unwrapped!))
print(crtMethodStr)
if crtMethodStr.hasPrefix("const") {
// call it
}
}
我得到的只是返回数组中的“init”?问题是什么?我在另一个线程上看到只需添加“@objc”就可以解决这个问题。另外,如何从重新调整的数组中访问这些变量之一?
【问题讨论】: