[swift-users] Coerce NSData to Data

Ryan Lovelett swift-dev at ryan.lovelett.me
Fri Oct 14 13:15:27 CDT 2016

I'm puzzeled by the behavior of the automatic coercion. Specifically
when something will work and when it will not. It at least has something
to do with the platform. That much I have tracked down.

I've attached a file, bridge.swift, that on Linux will fail to compile
with the error: cannot convert value of type 'NSData' to type 'Data' in
coercion. However, on Darwin it compiles just fine.

(The contents of the file just incase it gets stripped)

import Foundation

let md = NSData(bytes: [0x0D, 0x0A, 0x0D, 0x0A], length: 4)
_ = md as Data

On Linux the `md` has a method `_bridgeToSwift()` that returns the
`Data` I am looking for. But of course, the Darwin version does not.

Which begs the question what is the cross-platform way of doing things?
I'm hoping it is not `if #os(Linux)`...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bridge.swift
Type: application/octet-stream
Size: 94 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161014/792dc464/attachment.obj>

More information about the swift-users mailing list