Tom
Tom

Reputation: 85

swift - if statement and array

I want to match a string from the DicX to a an existing title (title of a table which changes according to the cell selection).

var DicX = ["xx",
                 "yy",
                 "zz",
                 "qq"]

let DicYY = [["11", "22", "33", "44"],
               ["1", "2", "3", "4"],
               ["m", "n", "k", "b"],
               ["bb", "kk", "mm", "nn"]]

the title I'm comparing with is like this:

title = detailX.insideTitle

so I want that when the title string equal to one of the DicX strings, to show the corresponding strings for it in DicYY each one of the 4 on a button.

but can't get the match correct, I tried to do like:

var currentX = detailX.insideTitle
    if DicX == currentX["DicX"] {
}

I get this message :

Cannot subscript a value of type 'String' with an index of type 'String'

how can I do the if statement? and how to get the corresponding from DicYY?

Upvotes: 1

Views: 528

Answers (1)

brduca
brduca

Reputation: 3623

This will do the job (if i got it right).

import Foundation

let DicX = ["xx",
            "yy",
            "zz",
            "qq"]

let DicYY = [["11", "22", "33", "44"],
             ["1", "2", "3", "4"],
             ["m", "n", "k", "b"],
             ["bb", "kk", "mm", "nn"]]

let searchterm = "yy"

for (index, elem) in DicX.enumerated()
{
    if (searchterm != elem) { continue }
    print(DicYY[index]) // This will print ["1","2","3","4"]
}

Upvotes: 1

Related Questions