I need to send a NSSet over the network, so I need to convert it to Data.
I have tried this:
let setData : Data = withUnsafeBytes(of: mySet) { Data($0) }
without success.
Convert to NSData is easy but what about Data?
How do I do that?
Either convert it to a general-purpose serialization form (JSON or binary Plist) using Codable (if it needs to be parsed by another system in a portable format), or hand it to NSKeyedArchiver using NSCoder (if it needs to be read just by Cocoa apps, and you want to save the "set-ness" and any object relationships).
Details on NSKeyedArchiver are in the Archives and Serializations Programming Guide.
Details on Codable are in Encoding, Decoding, and Serialization.
NSSet isn't Codable directly, but that doesn't matter because JSON and plists don't support sets anyway. You can just convert it to an Array (or Set).
Since you said that converting to NSData is easy, maybe you're already aware of all this, and all you need is Martin's comment: add as Data
.