<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. </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 = ... // to declare a contiguous array. </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=""> because to a programmer [Swift any type] arrays are undefined whether to be </div><div style="color: rgb(0, 0, 0);" class="">be contiguous or not. 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> <span style="color: rgb(187, 44, 162);" class="">class</span> ByteArray // Note: Just a model for a lower level implementation! </div><div style="margin: 0px; line-height: normal;" class="">{</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(187, 44, 162);" class="">private</span> <span style="color: rgb(187, 44, 162);" class="">var</span> bytebuffer = [<span style="color: rgb(112, 61, 170);" class="">UInt8</span>]() // Not! Must be an internal contiguous byte array.</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class=""> </div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class=""> init</span>(contents: [<span style="color: rgb(112, 61, 170);" class="">UInt8</span>]) <span style="color: rgb(0, 132, 0);" class="">// Constructor.</span></div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: rgb(0, 0, 0);" class=""> </span><span style="color: rgb(187, 44, 162);" class="">for</span><span style="color: rgb(0, 0, 0);" class=""> byte </span><span style="color: rgb(187, 44, 162);" class="">in</span><span style="color: rgb(0, 0, 0);" class=""> contents </span>// load bytes into contiguous!! buffer</div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal;" class=""> <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=""> }</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(187, 44, 162);" class="">func</span> setByte(index index: <span style="color: rgb(112, 61, 170);" class="">Int</span>, byte: <span style="color: rgb(112, 61, 170);" class="">UInt8</span>) // set a byte in the buffer</div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(187, 44, 162);" class="">if</span> index.outsideRange(<span style="color: rgb(39, 42, 216);" class="">0</span>..<bytebuffer.count) // should be valid index</div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: rgb(0, 0, 0);" class=""> </span>// Aarrggh! throw: subscripting error</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class=""> bytebuffer[index] = byte</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""> // other functions could be added to clear, shift etc.</div><div style="margin: 0px; line-height: normal; min-height: 19px;" class=""> </div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(187, 44, 162);" class="">func</span> address() -> <span style="color: rgb(112, 61, 170);" class="">Any</span></div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="color: rgb(0, 0, 0);" class=""> </span><span style="color: rgb(187, 44, 162);" class="">return</span><span style="color: rgb(0, 0, 0);" class=""> </span>bytebuffer // use as someExternalFunc(bytes.address,…) </div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""> func length() -> Int</div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal;" class=""> return bytebuffer.count</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""> </div></div></div><div style="color: rgb(0, 0, 0);" class=""><br class=""></div><div style="color: rgb(0, 0, 0);" class="">// usage example: </div><div style="color: rgb(0, 0, 0);" class=""><div class=""><br class=""></div><div class="">let bytes = ByteArray(contents: someUInt8array )</div><div class=""><br class=""></div><div class=""><div class="">// or -other constructor- make a byte array of 512 zero bytes:</div><div class=""><br class=""></div><div class="">let bytes = ByteArray(length: 512, fillWith: 0) </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: bytes[idx] = value</div><div class=""><br class=""></div><div class="">var offset = 2 // load databytes</div><div class="">for b in midiData</div><div class="">{</div><div class=""> bytes.setByte(index: offset, byte: b)</div><div class=""> offset++ </div><div class="">} </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(&bytes.address, . . . ) </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 <<a href="mailto:jj@johanjensen.dk" class="">jj@johanjensen.dk</a>> 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: please take a look at this code: </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=""> static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:</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=""> // 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=""> // know of no other way to create a tuple with 256 elements...</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> 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=""> 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="">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="">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="">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=""> 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=""> <br class=""></div><div style="margin:0px;font-size:11px;line-height:normal" class=""> func midiSend(status: Int, val1: Int, val2 :Int)</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> {</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> var midipacket = MIDIPacket()</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=""> midipacket.timeStamp = 0</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> midipacket.length = 3</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> midipacket.data = midiDataTuple //<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<</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=""> midipacket.data.0 = UInt8(status)</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> midipacket.data.1 = UInt8(val1 )</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> midipacket.data.2 = UInt8(val2 )</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=""> var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)</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=""> MIDIReceived(midiSource, &midipacketlist)</div><div style="margin:0px;font-size:11px;line-height:normal" class=""> }</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="">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? </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=""> </div></div></div></div>
</div></blockquote></div><br class=""></body></html>