swift - if语句和数组 [英] swift - if statement and array

查看:140
本文介绍了swift - if语句和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将DicX中的字符串与现有标题(根据单元格选择而变化的表格标题)进行匹配。

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

所以我希望当标题字符串等于其中一个DicX字符串时,显示相应的字符串为DicYY按钮上的4个中的每一个。

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"] {
}

我收到此消息:


不能下标String类型的值,其索引类型为String

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

我该怎么做if语句?以及如何从DicYY获得相应的信息?

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

推荐答案

这将完成工作(如果我做对了)。

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"]
}

这篇关于swift - if语句和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆