[swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

Joe Groff jgroff at apple.com
Mon Jun 26 11:57:02 CDT 2017

> On Jun 26, 2017, at 1:55 AM, Daniel Vollmer via swift-users <swift-users at swift.org> wrote:
> Hi Rick,
>> On 26. Jun 2017, at 02:37, Rick Mann via swift-users <swift-users at swift.org> wrote:
> [snip]
>> I'd also like to avoid unnecessary copying of the data. All of it is immutable for the purposes of this problem.
>> How can I get the UInt16 that starts at byte X in a Data? Same goes for Double or Int32 or whatever.
> I’m not sure what Swift’s stance on this is, but not all platforms allow misaligned memory accesses (such as your attempt to access a UInt16 that lies at an odd memory address).

Unaligned memory accesses are not currently allowed by the language semantics, regardless of the underlying ISA. You should use memcpy if you need to load potentially-unaligned values out of raw memory.


More information about the swift-users mailing list