I filed a new JIRA for this, https://bugs.swift.org/browse/SR-10778, based on an old radar rdar://problem/28201395 Add Foundation Data API for moving typed values into and out of raw bytes. /cc @Philippe_Hausler, @itaiferber I would be nice to have this soon so people aren't always forced to understand how to work with unsafe pointers.
data happened to be misaligned to begin with.
I really would like to write:
let bigNum = UInt64(data: data)
let bigNum = UInt64(data: data, offset: 7)
and would expect nil (if data is too short) or a value.
I suppose extensions could be provided for the scalar types. I don't think it can replace a generic
copyBytes API. Note that Data APIs use indices instead of byte offsets. I also think it's more conventional for Swift libraries to trap on something the user would normally check for themselves rather than force the user to unwrap an optional.
Even better (as I am receiving data in network byte order) would be:
let bigNum = UInt64(data: data, endianness: .bigEndian) // converts from bigEndian to host endianness
I think the conventional way to do it is
Actually, I agree with your original suggestion. I added a note about this to the JIRA bug.