Is there a perl-ish like ability to dynamically create objects and actions?

Not sure if this is possible with swift, so here goes.


I have a string array , these are defined by the user, ie, NO a priori knowledge


for toolbar 1, arrayA:[String]=["f1", "f2", "f3".....]

for toolbar 2, arrayB:[String] =["f3", "f10", "f22"...]


I have a database of functions or matrix if you like for the functions.


to create a bunch of UIButtons is no big deal just loop through a class to define UIButtons, now the tricky part, I want to define actions take for each button as the array value.


a[0]-> create button, name it "f1", set a background image "f1.png", set a color, "f1" and call an action named f1

Answered by NotMyName in 90318022

Keep in mind that Swift is a compiled language, and Perl is usually an interpreted language.


So you can take this sequence:

a[0]-> create button, name it "f1", set a background image "f1.png", set a color, "f1" and call an action named f1

go through and write code to perform each of those actions under the appropriate conditions (either as one statically coded sequence, or as a sequence of independent steps), and then have

a[x] -> a token or sequence of tokens

and then process those tokens to achieve dynamic behavior. Having all of the buttons use the same action handler, figure out which element of the array they are, and then process the proper sequence of tokens is entirely feasible.


But you've got to define the various behaviors ahead of time, you don't get to just put arbitrary strings in a database and call eval() to do arbitary things.

without a ton of ifdef statements or a massive switch.



usually barButtons are defined like


func f1(barButtonItem: UIBarButtonItem){ "do stuff here"}


I see no way like perl does to convert the string into a virtual system command.

a[0]-> create button, name it "f1", set a background image "f1.png", set a color, "f1" and call an action named f1

It really depends on what you mean by action. In classic Cocoa terms that’d be a target/action pair, and you can certainly set that up via Swift. For example, in this code:

func buttonWithName(name: String) -> UIBarButtonItem {
    return UIBarButtonItem(title: name, style: .Plain, target: self, action: Selector(name + "Action:"))
}

override func viewDidLoad() {
    self.navigationItem.rightBarButtonItem = self.buttonWithName("f1")
}

func f1Action(sender: AnyObject) {
    NSLog("f1")
}

the

viewDidLoad()
method creates a bar button item that calls
f1Action()
via UIBarButtonItem’s target/action support.

A lot of other wacky dynamic things are possible in Swift, but you do have to get creative. If you’d like more hints, you should post a more concrete description of your requirements.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

Keep in mind that Swift is a compiled language, and Perl is usually an interpreted language.


So you can take this sequence:

a[0]-> create button, name it "f1", set a background image "f1.png", set a color, "f1" and call an action named f1

go through and write code to perform each of those actions under the appropriate conditions (either as one statically coded sequence, or as a sequence of independent steps), and then have

a[x] -> a token or sequence of tokens

and then process those tokens to achieve dynamic behavior. Having all of the buttons use the same action handler, figure out which element of the array they are, and then process the proper sequence of tokens is entirely feasible.


But you've got to define the various behaviors ahead of time, you don't get to just put arbitrary strings in a database and call eval() to do arbitary things.

Hi, thanks for the help.


Say I wanted build the command from variables?


for n in list

func $arrayA[n] (barButtonItem: UIBarButtonItem){ eval (`grab dB command strings for $A[n] `) }

done


=> which would return the evaluated swift code

func f22(barButtonItem: UIBarButtonItem){ "create bar button that does something "}

Thanks for the rely, good stuff.


I like eval().


ok so i used tag ids to create a non-uniform, somewhat sparse matrix.


is there an efficient way to use UIBarButtonItemGroup to allocate specific indices to specific toolbars?

Is there a perl-ish like ability to dynamically create objects and actions?
 
 
Q