Swift blocks not working

前端 未结 2 1174
轻奢々
轻奢々 2020-12-29 14:58

I\'ve been trying to figure out how to use JavaScriptCore in swift. I\'m running into problems however when I have to deal with blocks as arguments, seems like the block is

2条回答
  •  旧时难觅i
    2020-12-29 15:52

    I have a working demo at:

    • https://github.com/dankogai/swift-jsdemo

    And here is the part that implements block registration:

    typealias ID = AnyObject!
    extension JSContext {
        func fetch(key:NSString)->JSValue {
            return getJSVinJSC(self, key)
        }
        func store(key:NSString, _ val:ID) {
            setJSVinJSC(self, key, val)
        }
        func store(key:NSString, _ blk:()->ID) {
            setB0JSVinJSC(self, key, blk)
        }
        func store(key:NSString, _ blk:(ID)->ID) {
            setB1JSVinJSC(self, key, blk)
        }
        func store(key:NSString, _ blk:(ID,ID)->ID) {
            setB2JSVinJSC(self, key, blk)
        }
    }
    

    You need a very small objc code and bridging header to make it work. See the repository for details.

提交回复
热议问题