Before developing an ios (or osx) app, iâve decided to gradually discover the swift language. So i ask myself how to solve a problem, and if i find the solution i will post the code snippet. (sorry for my terrible English).
I want to load a javascript file and run its functions
importJavaScriptCore// get the home path directorylethomeDir=NSHomeDirectory()// load javascript file in StringletjsSource:String!=String.stringWithContentsOfFile(homeDir+"/mylib.js")// create a javascript context environment and evaluate scriptvarcontext=JSContext()context.evaluateScript(jsSource)// get reference to hello() functionlethelloFunc=context.objectForKeyedSubscript("hello")// execute hello() function with parameterlethelloValue=helloFunc.callWithArguments(["World!!!"])// get reference to hola() functionletholaFunc=context.objectForKeyedSubscript("hola")// execute hola() function with parameterletholaValue=holaFunc.callWithArguments(["Bobby"])println(helloValue)// print "Hello World!!!"println(holaValue)// print "Hola Bobby CĂłmo estĂĄs?"
âEt voilĂ !â. It seems easy, but if youâve never programmed with Objective-C, Cocoa, Foundation, ⊠This can be difficult ;).