primus-objc icon indicating copy to clipboard operation
primus-objc copied to clipboard

Using with Swift

Open cfr opened this issue 10 years ago • 3 comments

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))

cfr avatar Feb 03 '15 11:02 cfr

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?

nunofgs avatar Feb 03 '15 23:02 nunofgs

I wonder how to stream binary data using primus-objc in order to use Spark#pipe on server side...

danil-z avatar Feb 10 '15 04:02 danil-z

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

katopz avatar May 19 '15 09:05 katopz