Dynamically calling a method in Swift -


i've created library of pre-defined colours use in swift , objective-c , i've rewritten plug-in xcode previews colour in editor works library (along few other changes).

the plugin has list of colours that's created. i'm not thrilled solution since means anytime change list of colours have modify code here too. plus don't having data structure hanging around holding approximately 1500 colours , strings. doesn't seem efficient or elegant.

what i'd instead of storing of colours in memory call proper function of nscolor when comes across function name in code. i've found can check see if nscolor responds selector function respondstoselector: calling performselector: doesn't meet needs since need return value call.

so there way go this.

let colourdict [ string : nscolor ] = [     "blackcolor" : nscolor.blackcolor(),     "whitecolor" : nscolor.whitecolor() ] ...  let foundcolour = colourdict[ colourmethodname ] 

to this.

if ( ncolor.respondstoselector ( selector ( colourmethodname ) ) {     foundcolour = nscolor.performselector ( selector ( colourmethodname ) ) } 

obviously wouldn't performselector: function i'm looking type of functionality except returning nscolor object.

thanks.

i don't understand wrong aaron brager's original suggestion. start dictionary of functions:

let colourdict : [ string : () -> uicolor ] = [     "blackcolor" : uicolor.blackcolor,     "whitecolor" : uicolor.whitecolor,     // ... , on ... ] 

this extremely efficient - far more efficient generating colors in memory , holding them there - because it's list of pointers, basically.

to obtain actual color, obtain color function name, , call it:

let actualcolor = colourdict["blackcolor"]!() 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -