mercredi 29 août 2018

How to get number of method and it's name using reflection?

1). I have already try belowe code :

if let types = getTypesOfProperties(in: Book.self){
    for (name, type) in types {
        print("'\(name)' has type '\(type)'")
    }
 }

Book class :

class Book: NSObject {
    let title: String
    let author: String?
    let numberOfPages: Int
    let released: Date
    let isPocket: Bool

    init(title: String, author: String?, numberOfPages: Int, released: Date, isPocket: Bool) {
        self.title = title
        self.author = author
        self.numberOfPages = numberOfPages
        self.released = released
        self.isPocket = isPocket
    }
}

i used reflation frmawork but types evry time it's return nil.

2).I have also try second

let myClass = Car.self

        var methodCount: UInt32 = 0
        let methodList = class_copyMethodList(myClass, &methodCount)

        for i in 0..<Int(methodCount) {
            let selName = sel_getName(method_getName(methodList![i]))
            //  let methodName = String(CString: selName, encoding: NSUTF8StringEncoding)!
            print(selName)
        }

Car class :

class Car: NSObject { var brand:String var model:String var price :Double var year:Int init(brand:String,model:String,price:Double,year:Int){

    self.brand = brand
    self.model = model
    self.price = price
    self.year = year
    func f1() {}
    func f2() {}
    func f3() {}
   }
}

here method is 4 but count return only 1.

please help .





Aucun commentaire:

Enregistrer un commentaire