Jae Jae - 1 year ago 55
Swift Question

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

I want to be able to compare values from the

level instead of the
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
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
to a
? In other words, the String value that I get from indexing
like so
connectedNames[0][0] == "Some String"

Answer Source

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