Skip to content

Instantly share code, notes, and snippets.

@xpqz
Created October 13, 2014 09:38
Show Gist options
  • Save xpqz/01ee8c074338c0b80a7d to your computer and use it in GitHub Desktop.
Save xpqz/01ee8c074338c0b80a7d to your computer and use it in GitHub Desktop.
JavaScript from Swift
import Foundation
import JavaScriptCore
public class GeoWrapper {
var jsContext: JSContext
public init?() {
jsContext = JSContext()
jsContext.exceptionHandler = { NSLog("ERROR: '%@'", $1) }
if let path = NSBundle(forClass:GeoWrapper.self).pathForResource("GeoFunctions", ofType: "js") {
let content = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
let jsval = jsContext.evaluateScript(content)
} else {
return nil
}
}
public func convertOSGridRefToWSGS84(gridref: String) -> String? {
if let jsFunction = jsContext.objectForKeyedSubscript("convert") {
if let value = jsFunction.callWithArguments([ gridref ]) {
return value.toString()
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment