1

I am trying to get method list of a class. Here's my code

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!)))
}

Output is :

init 

method1 and method2 are not displaying in output.

Please correct me if i am doing something wrong. Help will be appriciated.

Thank You

Trool Destroyer
  • 207
  • 3
  • 8
  • Are you using Swift 4? Have a look at https://stackoverflow.com/questions/44390378/how-can-i-deal-with-objc-inference-deprecation-with-selector-in-swift-4 – Martin R Sep 26 '17 at 09:36

1 Answers1

1

You have to expose your methods to Objective C with the @objc attribute.

Like this:

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

  @objc func method2(){
    print("Method2")
  }
}
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71