Wednesday, 15 January 2014

swift - How to prevent a 'Array index out of range' error? -


Is there any way, if it is similar to using and / or optionals, then this To check if you are going to index empty buffer in Swift?

Determine your own:

  extension array {func ref (I: Int) - & gt; T? {Return <0 < = I & amp; Amp; I & lt; Count? The self [i]: nil}}  

ref () function returns an optional, so it can be zero , and If you can use the value returned from the if syntax to the value ref () , then you can use it. You will use it like this:

  var myA = [10,20,30] if val = myA.ref (index) {// Usage 'val' if index is & lt; 3} Other {// do so if the index is too high}  

No comments:

Post a Comment