I have a struct containing a struct and an NSObject
that I want to serialize into an NSData
object:
struct Packet {
var name: String
var index: Int
var numberOfPackets: Int
var data: NSData
}
var thePacket = Packet(name: name, index: i, numberOfPackets: numberOfPackets, data: packetData)
How do I best serialize the Packet into an NSData
, and how do I best deserialize it?
Using
var bufferData = NSData(bytes: & thePacket, length: sizeof(Packet))
of only gives me the pointers of name and data. I was exploring NSKeyedArchiver
, but then I'd have to make Packet an object, and I'd prefer to keep it a struct.
Cheers
Nik
It seems this came out recently, and to me it is looking solid. Didn't try it yet...
https://github.com/a2/MessagePack.swift
Well, Swift doesn't have any magical serialization method, if that's what you are after. Since the good days of C, when you have a struct with a pointer, that is a flag that you can't serialize the bytes of that struct's instance without following the pointers and fetching their data. Same applies to Swift.
Depending on your Serialization needs and constraints, I'd say using
NSCoding
or even JSON strings will tidy your code and make it more predictable than the current state. Sure, you'll need to write a mapper, and there is an overhead. Everyone will tell you this: "Measure first".Now, here is the interesting part:
If you really want to inline your data in that struct, and stream the contents without building the packet around
NSData
as you're doing, you can reserve bytes using SwiftTuples
, which work much like how you would reserve bytes in C usingchar[CONST]
:To expand a bit on this, I think it's pretty horrible, but possible. You can write to the tuple's memory location and read from it using something like this.
I used Jeff's example to create the following struct:
}
Like Dag mentioned the whole thing is a bit fragile. Sometimes the App crashes when the name contains a whitespace or an underline/underscore, and sometimes it crashes just without reason. In all cases the name which is unarchived looks similar to this '4\200a\256'. Surprisingly this is not a problem in case of season or episode (like in "Season 2"). Here the whitespace doesn't force the app to crash.
Maybe it's an alternative to encode the strings to utf8 but I'm not familiar enough with the archive/unarchive methods to adopt them for this case.
Not really getting any feedback, this is the solution I ended up with:
encode()
anddecode()
functions for my structInt
toInt64
so theInt
has the same size on 32-bit and 64-bit platformsData
, but onlyInt64
Here is my code, I would be very grateful for your feedback, especially if there are less cumbersome ways to do this:
Swift 3
This is an unaltered copy-paste from a Playground in Xcode 8.2.1 that works. It is a bit simpler than other answers.
Notes
I couldn't make
archive
andunarchive
instance methods becauseData.init(bytes:count:)
is mutating on thebytes
parameter? Andself
isn't mutable, so... This made no sense to me.The
WhizzoKind
enum is in there because that is something I care about. It's not important for the example. Someone might be paranoid about enums like I am.I had to cobble this answer together from 4 other SO question/answers:
And these docs: - http://swiftdoc.org/v3.1/type/UnsafePointer/
And meditating on the Swift closure syntax until I wanted to scream.
So thanks to those other SO askers/authors.
Update
So this will not work across devices. For example, sending from iPhone 7 to Apple Watch. Because the
stride
is different. The above example is 80 bytes on iPhone 7 Simulator but 40 bytes on Apple Watch Series 2 Simulator.It looks like the approach (but not syntax) by @niklassaers is still the only one that will work. I'm going to leave this answer here because it might help others with all the new Swift 3 syntax and API changes surrounding this topic.
Our only real hope is this Swift proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md