javascript - How can I invoke a class method in Swift if I only have the string? -
i writing cordova plugin healthkit , task make sure can dynamically read more or less data hk. im doing native part in swift. write javascript bit lost in swift part.
i want able dynamically invoke methods having string.
let store = hkhealthstore()      {         let bloodtype: hkbloodtypeobject = try store.bloodtype() ... that example read blood type. not swift developer, there way can dynamically, in javascript:
... // assuming function receives string paramater let param[0] = try store[param[0]]() // <-- how in swift? then can talk server , list of characteristics , load them dynamically healthkit without having update code , hardcode each possible characteristic.
create mapping parameter strings api enumeration.
enum api: string {     case function1 = "function1"     case function2 = "functiontwo"     case function3 = "threethreethree" } create relay function maps api enumeration swift function.
func callthecorrectfunction(_ api: api) {     switch api {     case .function1: updateblood()     case .function2: spinaround()     case .function3: jump()     } } construct function call using enum's rawvalue initializer.
let param = fromsomejsonstrings[0] let api = api(rawvalue: param) callthecorrectfunction(api) alternatively, use dictionary mapping [string: function] in same way.
typealias function = () -> () let dict = {     "function1": updateblood,     "function2": spinaround, } dict[param[0]]() 
Comments
Post a Comment