swift - Dynamically performing an action based on string passed in textfield -


say have textfield - inputtextfieldand 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:

  1. within performactiondynamically pass inputtextfield.text in nsselectorfromstring() obtain selector
  2. invoke performselector:withobject: on self perform respective function

the thing can think in implementing same behavior in swift -

  1. define 2 closures name firstfunc, secondfunc
  2. store closures in dictionary values keys - 'firstfunc' , 'secondfunc' respectively
  3. obtain respective closure dictionary based on value in textfield
  4. 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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -