2017-09-26 58 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") 
    } 
}