So, I'm doing some development in Swift, and I'm doing a small refactor to improve the internal logistics. I'm changing a class that was handling [UInt8]
variables to handle Data
objects instead. The problem: say I do something like this...
var data = Data()
var buffer = < an incoming Data object >
buffer.removeFirst()
buffer.removeFirst()
data = buffer[0..<32]
I get a hard EXC_BREAKPOINT error and the debugger tells me data
is represented by a slice
with range e.g. 2..<40
right now. I didn't have this problem with [UInt8]
, and it looks like .removeFirst()
doesn't reset Data
's index like it does for [UInt8]
.
I can get around this by, for example, doing buffer = Data(buffer)
to get a fresh index every time, but I don't feel great about that. Neither do I want to allow this buffer's index to increment forever, as it's going to handle an indefinite stream of incoming data.
With my limited Google-fu, I was unable to find confirmation of this difference, nevermind an explanation. I'd love to understand why .removeFirst
displays this difference in behaviour. I'd also love any tips, tricks etc that might come up. Originally, I was converting my incoming Data
to [UInt8]
and then back into Data
, and I'm hoping to get rid of those seemingly excessive conversions.