1

I'm trying to check whether a variable (or rather a particular index of an array) exists in Swift.

If I use

if let mydata = array[1] {

I get an error if the index has a value, and a crash if it doesn't.

If I use

if array[1] != nil {

I get compiler warnings and/or crashes.

Essentially, I'm just trying to get command line arguments (which are any filename) and check whether they have been included or not. All the examples for command line arguments I've seen use switch/case statements, but to check for known text, rather than varying filenames.

Im still getting Index out of range errors in Xcode with the following:

if arguments.count > 1 {
    var input = arguments[2]
} else {
}
benc
  • 1,381
  • 5
  • 31
  • 39
benwiggy
  • 1,440
  • 17
  • 35
  • 2
    You have to check if the array contains more than one item: `if array.count > 1 { let mydata = array[1] ...` – vadian Sep 08 '17 at 18:57

4 Answers4

5

try this:

extension Collection where Indices.Iterator.Element == Index {

    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

then:

if let value = array[safe: 1] {
    print(value)
}

now you can even do:

textField.text = stringArray[safe: anyIndex]

that would not cause a crash because textField.text can be nil, and [safe:] subscript always returns value if exists or nil if not exists

elisar4
  • 180
  • 1
  • 7
2
if index < myData.count {
  // safe to access 
  let x = myData[index]
}
Musa almatri
  • 5,596
  • 2
  • 34
  • 33
DoesData
  • 6,594
  • 3
  • 39
  • 62
  • Yes, that's certainly the obvious (Doh!) method. I'm intrigued as to how you're supposed to do it outside of an array, just for a general variable, though. My whole time with Swift is setting up contingencies for whether things exist or not. – benwiggy Sep 08 '17 at 19:10
  • 1
    I think you meant: index < myData.count – Musa almatri Sep 08 '17 at 19:29
1

Simply, To check for index :

if index < array.count {
// index is exist

let data = array[index]

}
Musa almatri
  • 5,596
  • 2
  • 34
  • 33
  • In the context of filenames: I'm looking to see whether the argument has been included, or not. If included, I don't know what filename someone will use, so can't use contains. But useful to know about contains nonetheless. – benwiggy Sep 08 '17 at 19:13
  • I have edited my answer, index should be less than the array.count unlike the selected answer where the index is greater than the array count! – Musa almatri Sep 08 '17 at 19:18
0

You can use contains method to check whether a value exists in an array or not.

For example:

let expenses = [21.37, 55.21, 9.32, 10.18, 388.77, 11.41]
let hasBigPurchase = expenses.contains { $0 > 100 } // hasBigPurchase is a boolean saying whether the array contains the value or not.

Check its documentation for more information.

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • 4
    That doesn't seem to be related to what the OP is asking. He's asking how to tell whether an index is valid. – Kevin Sep 08 '17 at 19:01