<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; -webkit-text-stroke-width: 0px; word-spacing: 0px; white-space: normal; text-transform: none; letter-spacing: normal;" class=""><div style="color: rgb(0, 0, 0);" class="">Hello Johan, thanks for your reply.&nbsp;</div><div style="color: rgb(0, 0, 0);" class="">It would be nice if there was some kind of attribute like</div><div style="color: rgb(0, 0, 0);" class="">var bytes: [UInt8] contiguous &nbsp;= ... &nbsp; &nbsp; &nbsp;// &nbsp;to declare a contiguous array.&nbsp;</div><div style="color: rgb(0, 0, 0);" class=""><br class=""></div><div style="color: rgb(0, 0, 0);" class="">Ok then, I'd suggest creating a new Swift type with an internal fixed length</div><div style="color: rgb(0, 0, 0);" class="">and contiguous byte array inside. Perhaps to be implemented in a Swift library.</div><div style="color: rgb(0, 0, 0);" class=""><br class=""></div><div style="color: rgb(0, 0, 0);" class="">This swift class example here is just a Model</div><div style="color: rgb(0, 0, 0);" class="">&nbsp;because to a programmer &nbsp;[Swift any type] &nbsp;arrays are undefined whether to be&nbsp;</div><div style="color: rgb(0, 0, 0);" class="">be contiguous or not. &nbsp;No error handling is implemented in this examplel)</div><div style="color: rgb(0, 0, 0);" class=""><div class=""><br class=""></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">final</span>&nbsp;<span style="color: rgb(187, 44, 162);" class="">class</span>&nbsp;ByteArray &nbsp; // Note: Just a model for a lower level implementation!&nbsp;</div><div style="margin: 0px; line-height: normal;" class="">{</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">private</span>&nbsp;<span style="color: rgb(187, 44, 162);" class="">var</span>&nbsp;bytebuffer = [<span style="color: rgb(112, 61, 170);" class="">UInt8</span>]() &nbsp; // Not! Must be an internal contiguous byte array.</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class="">&nbsp; &nbsp;&nbsp;</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">&nbsp; &nbsp; init</span>(contents: [<span style="color: rgb(112, 61, 170);" class="">UInt8</span>]) &nbsp;<span style="color: rgb(0, 132, 0);" class="">// Constructor.</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;{</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">for</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;byte&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">in</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;contents&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>// load bytes into contiguous!! buffer</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;{</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(79, 129, 135);" class="">bytebuffer</span>.<span style="color: rgb(61, 29, 129);" class="">append</span>(byte)</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">func</span>&nbsp;setByte(index index:&nbsp;<span style="color: rgb(112, 61, 170);" class="">Int</span>, byte:&nbsp;<span style="color: rgb(112, 61, 170);" class="">UInt8</span>) &nbsp; &nbsp; // set a byte in the buffer</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;{</div><div style="margin: 0px; line-height: normal;" class="">&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">if</span>&nbsp;index.outsideRange(<span style="color: rgb(39, 42, 216);" class="">0</span>..&lt;bytebuffer.count) &nbsp;// should be valid index</div><div style="margin: 0px; line-height: normal;" class="">&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;{</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>// Aarrggh! throw: subscripting error</div><div style="margin: 0px; line-height: normal;" class="">&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;bytebuffer[index] = byte</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; // other functions could be added to clear, shift etc.</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class="">&nbsp; &nbsp;&nbsp;</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">func</span>&nbsp;address() -&gt;&nbsp;<span style="color: rgb(112, 61, 170);" class="">Any</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;{</div><div style="margin: 0px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">return</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;</span>bytebuffer // &nbsp;use as someExternalFunc(bytes.address,…)&nbsp;</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; func length() -&gt; Int</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; return bytebuffer.count</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class="">&nbsp;</div></div></div><div style="color: rgb(0, 0, 0);" class=""><br class=""></div><div style="color: rgb(0, 0, 0);" class="">// usage example: &nbsp;</div><div style="color: rgb(0, 0, 0);" class=""><div class=""><br class=""></div><div class="">let bytes = ByteArray(contents: &nbsp; someUInt8array )</div><div class=""><br class=""></div><div class=""><div class="">// or -other constructor- &nbsp;make a byte array of 512 zero bytes:</div><div class=""><br class=""></div><div class="">let bytes = ByteArray(length: 512, fillWith: 0)&nbsp;</div></div><div class=""><br class=""></div><div class=""><div class=""><div class="">bytes.setByte(index: 0, byte: midiCommandByte)</div><div class=""></div></div></div><div class="">bytes.setByte(index: 1, byte: midiStatByte)</div><div class=""><br class=""></div><div class="">// define subscriptors? so that we could just write: &nbsp;bytes[idx] = value</div><div class=""><br class=""></div><div class="">var offset = 2 &nbsp; &nbsp; &nbsp; // load databytes</div><div class="">for b in midiData</div><div class="">{</div><div class="">&nbsp; &nbsp; bytes.setByte(index: offset, byte: b)</div><div class="">&nbsp; &nbsp;offset++ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div class="">} &nbsp;&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""></div><div class="">// pass the address of the contiguous byte buffer within (this instance of) ByteArray to some external function:</div><div class=""><br class=""></div><div class="">someExternalFunc(&amp;bytes.address, . . . )&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><br class=""></div></div><div class="">Regards</div><div class="">Ted</div><div class=""><br class=""></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><div><blockquote type="cite" class=""><div class="">On 14.02.2016, at 20:26, Johan Jensen &lt;<a href="mailto:jj@johanjensen.dk" class="">jj@johanjensen.dk</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><div style="margin:0px;font-size:11px;line-height:normal" class="">4 Tuples:&nbsp; please take a look at this code: &nbsp;</div><div style="margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp;static var z: UInt8 = 0&nbsp; // initalize tuple with 256 UInt8 values, bytes:</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; // Silly: why not an array instead of this.. a tuple is needed.. length must be exact 256..</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; // know of no other way to create a tuple with 256 elements...</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp;z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,&nbsp;</div><div style="margin:0px;font-size:11px;line-height:normal" class="">z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,&nbsp;</div><div style="margin:0px;font-size:11px;line-height:normal" class="">z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,&nbsp;</div><div style="margin:0px;font-size:11px;line-height:normal" class="">z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp;z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; func midiSend(status: Int, val1: Int, val2 :Int)</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; {</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; var midipacket = MIDIPacket()</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.timeStamp = 0</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.length&nbsp; &nbsp; = 3</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.data&nbsp; &nbsp; &nbsp; = midiDataTuple&nbsp; //&lt;-=&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.data.0 = UInt8(status)</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.data.1 = UInt8(val1&nbsp; )</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; midipacket.data.2 = UInt8(val2&nbsp; )</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; &nbsp; &nbsp; MIDIReceived(midiSource, &amp;midipacketlist)</div><div style="margin:0px;font-size:11px;line-height:normal" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.</div><div style="margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class="">IfTrue: Why are tuples the only data type to use for unmanaged byte arrays?&nbsp;</div></div></div></blockquote><div class=""><br class=""></div><div class="">There's currently some work on a proposal for <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007984.html" target="_blank" class="">Contiguous Variables (A.K.A. Fixed Sized Array Type)</a> which should help with the creation of tuples. (It can be read in a better fashion on <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809" target="_blank" class="">gmane</a>)<br class=""><br class=""></div><div class="">— Johan<br class=""></div><div class="">&nbsp;</div></div></div></div>
</div></blockquote></div><br class=""></body></html>