<div dir="ltr">







<p class=""><span class="">An working example,</span></p><p class=""><span class="">let</span><span class=""> fm = </span><span class="">NSFileManager</span><span class="">.</span><span class="">defaultManager</span><span class="">()</span></p>
<p class=""><span class="">  </span><span class="">var</span><span class=""> mh = </span><span class="">mach_header</span><span class="">()   // Defined as C struct, &lt;mach-o/loader.h&gt;</span></p>
<p class=""><span class="">  </span><span class="">var</span><span class=""> lc = </span><span class="">load_command</span><span class="">()  </span>// Defined as C struct &lt;mach-o/loader.h&gt;</p>
<p class=""><span class="">  </span><span class="">var</span><span class=""> location = </span><span class="">0</span></p>
<p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">let</span><span class=""> data = fm.</span><span class="">contentsAtPath</span><span class="">(&quot;home/user/projects/some.o&quot;) {</span></p>
<p class=""><span class="">    data.</span><span class="">getBytes</span><span class="">(&amp;mh, length: </span><span class="">sizeof</span><span class="">(</span><span class="">mach_header</span><span class="">))</span></p>
<p class=""><span class="">    location += </span><span class="">sizeof</span><span class="">(</span><span class="">mach_header</span><span class="">)</span></p>
<p class=""><span class="">    data.</span><span class="">getBytes</span><span class="">(&amp;lc, range: </span><span class="">NSRange</span><span class="">(location: location, length: </span><span class="">sizeof</span><span class="">(</span><span class="">load_command</span><span class="">)) )</span></p>
<p class=""><span class="">    location += </span><span class="">sizeof</span><span class="">(</span><span class="">load_command</span><span class="">)</span></p><p class=""><span class="">}</span></p><p class=""><span class="">-Rk</span></p></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 19, 2016 at 10:52 PM, Joe Groff via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
&gt; On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; I would like to know if a struct in Swift has any guarantee of contiguity, like a struct in C.  I am attempting to port a C program that makes many assumptions about the underlying semantics of C structs, and does things like overlaying an array over the struct using a cast, and then performs and cryptographic hashing of the entire struct by treating it as an array.<br>
&gt;<br>
&gt; 1.  I a Swift struct contiguous?<br>
&gt;<br>
&gt; 2.  If 1 above is true, can I somehow cast an array over the struct?<br>
<br>
</span>Swift structs have unspecified layout. If you depend on a specific layout, you should define the struct in C and import it into Swift for now.<br>
<br>
-Joe<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>