<div dir="ltr">yikes there’s no less verbose way to do that? and if the type isn’t an integer there’s no way to avoid the default initialization? Can this be done with opaques or something?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 8, 2017 at 7:04 PM, Johannes Weiß <span dir="ltr">&lt;<a href="mailto:johannesweiss@apple.com" target="_blank">johannesweiss@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Kelvin,<br>
<div><div class="h5"><br>
&gt; On 8 Nov 2017, at 4:54 pm, Kelvin Ma via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; According to the docs, the load(fromByteOffset:as:) method requires the instance to be “properly aligned” Does that mean if I have raw data meant to be interpreted as<br>
&gt;<br>
&gt; 0      1      2      3      4      5      6<br>
&gt; [ Int8 |    Int16    |    Int16    | Int8 | Int8 ]<br>
&gt;<br>
&gt;<br>
&gt; i can’t just load the Int16 from byte offset 1?<br>
<br>
</div></div>you can&#39;t just dereference that pointer as an Int16 (in any language) without causing UB but it&#39;s not really an issue, just do this (assuming `ptr` is the pointer into your data and `index` is the index to where your Int16 lives):<br>
<br>
<br>
    var value: Int16 = 0<br>
    withUnsafeMutableBytes(of: &amp;value) { valuePtr in<br>
        valuePtr.copyBytes(from: UnsafeRawBufferPointer(start: ptr.baseAddress!.advanced(by: index),<br>
                                                        count: MemoryLayout&lt;Int16&gt;.size))<br>
    }<br>
<br>
you can have the whole thing generic too for &lt;T: FixedWidthInteger&gt;<br>
<br>
    var value: T = 0<br>
    withUnsafeMutableBytes(of: &amp;value) { valuePtr in<br>
        valuePtr.copyBytes(from: UnsafeRawBufferPointer(start: ptr.baseAddress!.advanced(by: index),<br>
                                                        count: MemoryLayout&lt;T&gt;.size))<br>
    }<br>
<br>
-- Johannes<br>
<br>
<br>
&gt; ______________________________<wbr>_________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br>
</blockquote></div><br></div>