[swift-users] Mapping C semantics to Swift

Ken Burgett kenb at iotone.io
Fri May 20 09:33:39 CDT 2016

I have been told that the in-memory layout of a Swift struct is 
'undefined', so the C style of aliasing an array over a struct and 
iterating across the struct is a definite no-no.  Not surprising, since 
aliasing violates all type-safe rules.

So, if you have this situation, you must address it on a case-by-case 
basis.  In my case, converting a hash algorithm from C to Swift, the 
required C struct morphs into a Swift class, with a member function 
'as_C_byte_array' which answers an array of the required form.  
Internally, this function will shift and mask class attributes in order 
to build the necessary result.
Ugly, but I must preserve the semantics exactly if I hope to generate 
the exact same hash as the C version.

Ken Burgett
Principal Software Engineer
Email: kenb at iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co

More information about the swift-users mailing list