swift - Dynamically performing an action based on string passed in textfield -
say have textfield - inputtextfield
and button on ui action performactiondynamically
, in same class define 2 functions: 1. firstfunc
, 2. secondfunc
, want achieve behavior:
if user types "firstfunc" in textfield , taps on button should invoke firstfunc
function, , if types "secondfunc" in textfield , taps on button should invoke secondfunc
function.
in objective-c have achieved following below pseudocode:
- within
performactiondynamically
passinputtextfield.text
innsselectorfromstring()
obtain selector - invoke
performselector:withobject:
on self perform respective function
the thing can think in implementing same behavior in swift -
- define 2 closures name
firstfunc
,secondfunc
- store closures in dictionary values keys - 'firstfunc' , 'secondfunc' respectively
- obtain respective closure dictionary based on value in textfield
- invoke obtained closure
is there better way achieve intended behavior? please guide.
you try this:
if self.respondstoselector(selector(selectorstring)) { nstimer.scheduledtimerwithtimeinterval(nstimeinterval(0), target: self, selector: selector(inputtextfield.text), userinfo: nil, repeats: false) }
this done callback, have take account.
you use nsthread instead of nstimer if prefer:
nsthread.detachnewthreadselector(selector(inputtextfield.text), totarget: self, withobject: nil)
Comments
Post a Comment