aejhyun
aejhyun

Reputation: 612

How to compare the String value of a [[String]] to a String?

I want to be able to compare values from the [String] level instead of the String level. Here's what I mean:

var connectedNames:[[[String]]] = [[[]]]

for var row: Int = 0; row < connectedNames[0].count; row++ {
    if self.connectedNames[0][row] as! String == "asdf" {

    }
}

But the cast here from [String] to String fails so I can't make this value comparison.

So the main problem is this: Is there anyway to compare the String value of a [[String]] to a String? In other words, the String value that I get from indexing connectedNames like so connectedNames[0][0] == "Some String"?

Upvotes: 0

Views: 75

Answers (1)

Christopher Harris
Christopher Harris

Reputation: 682

You can only compare [[String]] to String by using the subscript method of the Array to access the inner element. This would work:

func compare() -> Bool {
    let arr: [[String]] = [["foo"]]
    let str: String = "foo"

    guard let innerArr = arr[0] else {
      return false
    }

    guard let element = innerArr[0] else {
      return false
    }

    return element == str
}

Upvotes: 2

Related Questions