简体   繁体   English

Swift 3如何在字典中使用单值获取不同的键

[英]How to get different key with single value in dictionary by Swift 3

I have a clickable UILabel. 我有一个可点击的UILabel。
And the label text show all dictionary values. 标签文本显示所有字典值。
Then when I click, I want to use value to check back the keys. 然后,当我单击时,我想使用值来检查键。
But if I just have a single value, how to separate keys to each different value label. 但是,如果我只有一个值,那么如何将键分隔到每个不同的值标签。

像这样的图像。

//Dictionary Data
self.finalDic = ["11defba4d092ggf00e0fdb5": "@user5 ", "114defba4d092ggf00e0fdb6": "@user6 ", "114defba4d092ggf00e0fdb1": "@user1 ", "114defba4d092ggf00e0fdb3": "@user1 ", "114defba4d092ggf00e0fdb4": "@user4 ", "114defba4d092ggf00e0fdb2": "@user1 "]

When I click the first @user1, I will get a key(114defba4d092ggf00e0fdb1). 当我单击第一个@ user1时,我将获得一个密钥(114defba4d092ggf00e0fdb1)。
Then I click the second @user1, I will get another key(114defba4d092ggf00e0fdb3). 然后单击第二个@ user1,我将获得另一个密钥(114defba4d092ggf00e0fdb3)。
Final I click the third @user1, I will get the last key(114defba4d092ggf00e0fdb2). 最后,我单击第三个@ user1,我将得到最后一个键(114defba4d092ggf00e0fdb2)。

What should I do to distribute them to a different label? 我应该怎么做才能将它们分配给其他标签?

label.handleMentionTap({ (string) in

        let keys = self.finalDic.allKeys(forValue: "@\(string) ")
        if keys.count > 1 {
            print("--> keys > 1 : \(keys)")
        }else{
            print("--> keys: \(keys)")
        }
})

You can also make and array which have multiple key-value pairs, and them you can give tags to your label and on tap you can get the value directly based on a tag. 您还可以对具有多个键-值对的make和array进行设置,并可以将标签赋予标签,然后点击即可直接基于标签获取值。

So your dataArray will be like this : 因此,您的dataArray将如下所示:

let dataArray = [ ["11defba4d092ggf00e0fdb5": "@user5 "], ["114defba4d092ggf00e0fdb6": "@user6 "], ["114defba4d092ggf00e0fdb1": "@user1 "], ["114defba4d092ggf00e0fdb3": "@user1 "], ["114defba4d092ggf00e0fdb4": "@user4 "], ["114defba4d092ggf00e0fdb2": "@user1 "]]

Anf you can run a for loop to show all the values on your label : 您可以运行for循环以显示标签上的所有值:

for i in 0..<dataArray.count{
    yourLabel.tag = i
    for (key,value) in dataArray[i]{
        print(key, value)
        yourLabel.text = value
    }

}

On tap gesture you can get the tag of the label and thus you can get the tapped value from your dataArray based on your tag. 在轻击手势上,您可以获得标签的标签,因此可以基于标签从dataArray中获得敲击值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM