<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div><span></span></div><div><div></div><div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">Ah ok, I was using the other version which distinguishes between nil and Optional(nil):</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">extension<span class=""> </span><span class="">Array</span><span class=""> {</span></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">subscript</span>(ifExists idx: <span class="">Index</span>) -> <span class="">Element</span>? {</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">get</span> { <span class="">return</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx ? <span class="">self</span>[idx] : <span class="">nil</span> }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">set</span> { <span class="">if</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx && newValue != <span class="">nil</span> { <span class="">self</span>[idx] = newValue! } }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">}</span></font></div></div></div></div></div></div></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">Where you can assign Optional(nil) to set a value to nil which is admittedly not very intuitive...</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = Optional(nil)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = array[ifExists: 1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[Optional(1)]"</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">A solution would probably be to introduce another enum mimicking Optional but built for the array subscript which allows to distinguish both cases.</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">-Thorsten </span></div><div><br>Am 06.02.2016 um 21:52 schrieb Maximilian Hünenberger <<a href="mailto:m.huenenberger@me.com">m.huenenberger@me.com</a>>:<br><br></div><blockquote type="cite"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = nil // sets array[0] to nil if index is valid</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;">array = [1]</div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = array[ifExists: 1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[nil]"</span></div></blockquote></div></div></body></html>