primus-objc
primus-objc copied to clipboard
Using with Swift
Here is the short sample:
https://gist.github.com/cfr/dfcb50accd64014a0019
Obviously we need some kind of wrapper to avoid casting:
let data: @objc_block (NSDictionary, AnyObject) -> () = { (d: NSDictionary, raw: AnyObject) -> () in
NSLog("Got data: \(d)")
}
p.on("data" as AnyObject!, listener: unsafeBitCast(data, AnyObject.self))
Wow, that's really cool. I must admit I've only played around with Swift for a bit so I don't feel comfortable porting primus to Swift yet. That said, I'd love to see someone contribute a Swift wrapper.
Anyone care to give it a shot?
I wonder how to stream binary data using primus-objc in order to use Spark#pipe on server side...
Here's what I got for now
class Block<T> {
let f : T
init (_ f: T) { self.f = f }
var casted: AnyObject { get { return unsafeBitCast(f, AnyObject.self) } }
}
Then
primus.on("open", listener : Block<@objc_block () -> ()> {
let _ = primus.write(["event": "roomAdd", "room": "defaultRoom"])
}.casted)
primus.on("data", listener : Block<@objc_block (NSDictionary, AnyObject) -> ()> { (d: NSDictionary, raw: AnyObject) in
NSLog("Got data: \(d)")
}.casted)
And not sure we could use some trick from this? https://github.com/pNre/ExSwift/blob/master/ExSwift%2FExSwift.swift#L249