Fast array index variable

Below is the error code. Is there a way to just return this value?

var testArray: [Int]?

testArray = [1,2,3]

testArray?[9]

      

+3


source to share


4 answers


There are three possible options here:

  • An optional array - that is, a variable that can hold an array of integers: [Int]?

  • An options array is an array containing variables that can be integers: [Int?]

  • Subscribers returning optional because, perhaps, this index is not valid, then there is an index that can return an integer if the index is valid: subscript(index) -> Int?

    .

In your example, you have the first case - an optional array. That is, either it testArray

is nil

, or it is a valid array containing zero or more elements.

To access anything inside the optional, you must first deploy it. You can either force-deploy it with !

(try not to do this unless you have a really good reason - no, that's not a reason). Or you can expand it conditionally, using if let

, or using one of the operators such as ?

, ?.

, ??

, etc. In this case it testArray?[9]

means "if testArray nil

then nil

, otherwise {Some whatever is at position 9}

. But there is no value in position 9 - it's just a 3-element array. So you get a runtime assertion."

(Another way of writing testArray?[9]

would be testArray.map { $0[9] }

where inside a block the array is unpacked and valid, but if its nil

block is never executed. But the result is still that you are trying to access the 9th element of the array and this is not allowed and you get runtime error)



The second case, an array of complementary integers, would mean that you could access testArray[1]

and you will return {Some 2}

because the array contains complementary ones. But again, you cannot access the 9th element because there is no ninth element.

Finally, the third case is where the calling index returns you an optional value, and if it is not a valid index, you return nil

. This seems to be what you expected. Fast arrays don't do this. Quick dictionaries if you're looking for a key. If the key matters, you get back {Some value}

, if not, you get nil

. The abbreviated rationale for this is that dictionaries contain sparse data while arrays contain dense data.

If you're interested, I've written posts about arguments and cons for creating arrays to return options from an index. But for now, like the other answers have suggested, you should check your boundaries before accessing the elements. You might want to try some helper methods, such as first

, last

, find

, and map

, and filter

, and so on, but not directly access the elements, because it leads to the fact that many of these problems go away.

(another interesting thing to think about is that all three cases above can be chained together. So you can have an additional dictionary of options that return options when you access it. If you're still vague about this , try playing around with this in the playground :)

+7


source


You must explicitly do range checking, for example:

var testArray: [Int]?
testArray = [1,2,3]

let index1 = 9
let numberNine : Int? = index1 < testArray!.count ? testArray![ index1 ] : nil
println(numberNine)

let index2 = 2
let numberTwo : Int? = index2 < testArray!.count ? testArray![ index2 ] : nil
println(numberTwo!)

      



The conditional ... ?

... :

... will check the condition before deciding which side :

to evaluate, therefore testArray![ index1 ]

protected by the condition: it will not throw an exception when the index is out of range.

+2


source


If you want to know if an array contains a specific value, you can use a global function contains

:

var testArray: [Int]?
testArray = [1,2,3]

if let actualTestArray = testArray {
    let doesContain9 = contains(actualTestArray, 9)
    println(doesContain9)  // prints "false"
}

      

If you can make testArray

it optional, you can simply do

var testArray: [Int]
testArray = [1,2,3]
let doesContain9 = contains(testArray, 9)
println(doesContain9)  // prints "false"

      

+1


source


When trying to access an index out of order in Swift, you should get an error, for example EXC_BAD_INSTRUCTION

at runtime.

You might want to use NSDictionary

. In fact, with a dictionary, you won't have any kind of runtime error when trying to access a key that doesn't exist. It will just returnnil

If you want to use an array, you need to check the number of elements you have in your array before accessing it using count

your array property

0


source







All Articles