1

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?

6
  • 1
    if DicX == currentX["DicX"] { } Commented Jun 21, 2018 at 10:39
  • 2
    DicYY and DicX are arrays not dictionaries Commented Jun 21, 2018 at 10:40
  • it doesn't work, its ==, just here I made a mistake in the posting. I'll edit it. Commented Jun 21, 2018 at 10:45
  • @Tom Because as Sh_Khan said, DicX and DicYY are arrays. Not dictionaries. And you can not access an array with currentX["DicX"]. Commented Jun 21, 2018 at 10:48
  • so how do I do it? Commented Jun 21, 2018 at 10:49

1 Answer 1

1

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"]
}
Sign up to request clarification or add additional context in comments.

2 Comments

that could work, I want the 'searchterm' to be equal to the title at that moment from 'title = detailX.insideTitle', how is that done? thank you!!!
let searchterm = detailX.insideTitle

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.