<分区>
我有一本字典,但我需要按特定值进行查询以获取键。但是好像 swift 没有 valueforKey 函数。
例子:
print(myDictionary["key"])
//output: 100
//but I want to get:
print(myDictionary.valueForkey:"100")
//output: key
你们中有人知道我怎样才能快速做到这一点吗?
<分区>
我有一本字典,但我需要按特定值进行查询以获取键。但是好像 swift 没有 valueforKey 函数。
例子:
print(myDictionary["key"])
//output: 100
//but I want to get:
print(myDictionary.valueForkey:"100")
//output: key
你们中有人知道我怎样才能快速做到这一点吗?
最佳答案
当然你可以在 Swift 中这样做,因为字典可以用元组表示:
let myDictionary = ["foo": 100, "baz" : 200]
if let found = myDictionary.filter({ $0.1 == 100 }).first {
let key = found.0
print(key) // "foo"
}
关于 swift : Query Dictionary using value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540238/
相关文章:
ios - 通过 Cocoapods 设置 CVCalendar : Views Aren't Showing
ios - '[NSObject : AnyObject ]' is not identical to ' NSDictionary', RKPathMatcher 问题
ios - NSFetchedResultsController 'didChange' 使用新的 CollectionDiffing 会导致奇怪的更改操作,从而导致崩溃
java - Hibernate 中具有两个多对一键的复合键