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 {
}
1
  • 2
    You have to check if the array contains more than one item: if array.count > 1 { let mydata = array[1] ... Commented Sep 8, 2017 at 18:57

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

2
if index < myData.count {
  // safe to access 
  let x = myData[index]
}

2 Comments

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.
I think you meant: index < myData.count
1

Simply, To check for index :

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

let data = array[index]

}

2 Comments

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.
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!
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.

1 Comment

That doesn't seem to be related to what the OP is asking. He's asking how to tell whether an index is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.