Hi. I'm writing a program that used to use (NS)InputStream to read a file, and I'm upgrading it to use DispatchIO. WWDC 2013 #704 showed an example of how to do this, but of course that was pre-Swift. You pass an Objective-C block which accepts a dispatch_data_t.
In Swift, I've got DispatchData. The rest of my program expects Data. How do I convert this?
First, I tried the simplest possible solution: "my_dd as! Data". The compiler warns that this always fails, and that's fair. They're different types.
Then I found some random person on the internet doing "my_dd as AnyObject as! Data", and that seems to work for me so far. But DispatchData may be non-contiguous, and Data conforms to ContiguousBytes. Am I just getting lucky that my program/data/memory usage today happen to only be making contiguous blocks today?
There's a note in WWDC 2013 #205 that (in Objective-C) dispatch_data_t can be cast to NSData. There's a note in the Swift.Data docs that it's bridged to NSData, so you can use those two types "interchangeably". But I'm having trouble finding details on exactly how casting and bridging works -- DynamicCasting.md has "XXX TODO EXPLAIN" in the section I care about!
I noticed that "my_dd as Any as! Data" also seems to work. Since dispatch_data_t and NSData are (Objective-C) NSObjects, and DispatchData and Data are (Swift) structs, are these two casting chains equivalent, and simply doing the conversion after bridging, or vice versa?
Is "my_dispatch_data as AnyObject as! Data" safe? If not, what's the best way to do this?