Skip to content Skip to sidebar Skip to footer

Swift Blocks Not Working

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 run immed

Solution 1:

It has something to do with how Swift implement closure. You need to use @convention(block) to annotate that the closure is ObjC block. Use unsafeBitCast to force cast it

var block : @convention(block) (NSString!) -> Void= {
   (string : NSString!) -> Voidin 
    println("test")
}

ctx.setObject(unsafeBitCast(block, AnyObject.self), forKeyedSubscript: "test")

from REPL

swift
Welcome to Swift!  Type :help for assistance.
  1> import Foundation
  2> var block : @convention(block)(NSString!) -> Void = {(string : NSString!) -> Voidin println("test")}
block: @convention(block)(NSString!) -> Void =
  3> var obj: AnyObject = reinterpretCast(block) as AnyObject
obj: __NSMallocBlock__ = {} // familiar block type

Solution 2:

I have a working demo at:

And here is the part that implements block registration:

typealiasID=AnyObject!
extensionJSContext {
    funcfetch(key:NSString)->JSValue {
        return getJSVinJSC(self, key)
    }
    funcstore(key:NSString, _val:ID) {
        setJSVinJSC(self, key, val)
    }
    funcstore(key:NSString, _blk:()->ID) {
        setB0JSVinJSC(self, key, blk)
    }
    funcstore(key:NSString, _blk:(ID)->ID) {
        setB1JSVinJSC(self, key, blk)
    }
    funcstore(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.

Post a Comment for "Swift Blocks Not Working"