0

I have a clickable 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.

Image like this.

//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).
Then I click the second @user1, I will get another key(114defba4d092ggf00e0fdb3).
Final I click the third @user1, I will get the last key(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)")
        }
})
tolerate_Me_Thx
  • 387
  • 2
  • 7
  • 21
  • why not swap key and value in your map? if so, you got value ref, and no need to make a key counter – vg0x00 Jan 17 '18 at 06:46
  • So do you have just one single label which holds all the users, or do you have several labels, each for one user? – Andreas Oetjen Jan 17 '18 at 06:47
  • okay, I want to show serveral label(@user1), and they have different key. – tolerate_Me_Thx Jan 17 '18 at 06:53
  • @vg0x00 ["@user1 ": "114defba4d092ggf00e0fdb1", "@user1 ":"114defba4d092ggf00e0fdb3", "@user1 ":"114defba4d092ggf00e0fdb2"] just like this have the same key? Is it legal? – tolerate_Me_Thx Jan 17 '18 at 06:59
  • you don't need to display all the key value in your label, like this: "@user1.(hash1)", "@user1.(hash2)", "@user1.(hash3)" only display the first 5 characters: "@user1, @user1, @user1" – vg0x00 Jan 17 '18 at 07:08
  • I know but a label text just "@user1 @user1 @user1", how to find back the key. – tolerate_Me_Thx Jan 17 '18 at 07:18
  • And they don't have repeat key. – tolerate_Me_Thx Jan 17 '18 at 07:20
  • Is this related to this question https://stackoverflow.com/questions/48297093/swift-how-to-make-uilabel-clickable-like-hashtag-and-distinguish-the-same-text#comment83580899_48297093 ? Seems to have the same issue. As I said there: https://stackoverflow.com/questions/48297093/swift-how-to-make-uilabel-clickable-like-hashtag-and-distinguish-the-same-text#comment83580899_48297093 Because with `label.handleMentionTap({ (string)` there is NO WAY to know if it's the first or second one `@user1` you tapped on, not even talking about your dictionary (which are not ordered!) – Larme Jan 17 '18 at 10:26

1 Answers1

0

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.

So your dataArray will be like this :

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 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.

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50