2017-09-26 59 views
1

我想获得一个类的方法列表。这里是我的代码“class_copyMethodList”只返回init方法

class MyClass: NSObject { 
    func method1(){ 
    print("Method1") 
    } 

    func method2(){ 
    print("Method2") 
    } 
} 

var methodCount: UInt32 = 0 
let methodList = class_copyMethodList(MyClass.self, &methodCount) 

for i in 0..<Int(methodCount){ 
    let unwrapped = methodList?[i] 
    print(NSStringFromSelector(method_getName(unwrapped!))) 
} 

输出是:

init 

method1method2则无法在输出显示。

请纠正我,如果我做错了什么。帮助将被appriciated。

谢谢

+0

您在使用雨燕4?看看https://stackoverflow.com/questions/44390378/how-can-i-deal-with-objc-inference-deprecation-with-selector-in-swift-4 –

+0

非常感谢你 –

回答

0

你必须与@objc属性暴露你的方法,以目标C。

像这样:

class MyClass: NSObject { 
    @objc func method1(){ 
    print("Method1") 
    } 

    @objc func method2(){ 
    print("Method2") 
    } 
}