Skip to content

Instantly share code, notes, and snippets.

@zeitiger
Last active March 30, 2022 08:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zeitiger/1387f7d996f64b493608 to your computer and use it in GitHub Desktop.
Save zeitiger/1387f7d996f64b493608 to your computer and use it in GitHub Desktop.
JSExportAs Workaround in Swift (simplified code example)
public typealias myFunctionDefAlias = (@objc_block (String, String, NSNumber) -> Void)
@objc
public protocol MyJSExportProtocol: JSExport {
// JSExportAs alternative in Swift via closure
var myFunction:myFunctionDefAlias? {get}
}
@objc
public class MyJSExportClass: NSObject, MyJSExportProtocol {
public var myFunction:myFunctionDefAlias?
public func init() {
self.myFunction = { [unowned self] (foo:String, bar:String, baz: NSNumber) in
self.myFunctionImpl(foo, bar: bar, baz: baz)
}
}
public func myFunctionImpl(foo: String, bar: String, baz: Bool) {
}
}
@brendand
Copy link

I'm getting the error: @objc_block is an unknown attribute

Wish a workaround wasn't needed for JSExportAs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment