检查字典是否包含 Swift 中的值
Check if dictionary contains value in Swift
只是简单的任务。我有一个字典 var types = [Int : String]()
,它初始化时像一个空的,在一些用户操作后它填充了数据。根据本词典中的空虚或某些特定数据,我 enable/disable UI 中的一个按钮。
检查是否为空很容易,但是如何检查字典是否包含某个值呢?
编译器建议我使用谓词占位符:
types.contains(predicate: ((Int, String)) throws -> Bool>)
由于您只想检查给定的 值 是否存在,您可以对字典的 values
属性应用 contains
方法(给定本机 Swift 字典),例如
var types: [Int : String] = [1: "foo", 2: "bar"]
print(types.values.contains("foo")) // true
如所述,使用字典的values
属性貌似会产生开销(我自己没有验证过)w.r.t。只需根据每个 Dictionary
元素的键值元组中的值条目直接检查 contains
谓词。因为 Swift 很快,所以这应该不是问题,但是,除非你正在使用一个巨大的字典。无论如何,如果您想避免使用 values
属性,您可以查看上述答案中给出的替代方案,或者使用另一个替代方案(Dictionary
扩展名)作为如下:
extension Dictionary where Value: Equatable {
func containsValue(value : Value) -> Bool {
return self.contains { [=11=].1 == value }
}
}
types.containsValue("foo") // true
types.containsValue("baz") // false
我写了一个在字典上使用 contains
方法的函数。
您的具体情况:
let dic : [Int : String] = [1 : "a", 2 : "b"]
func dictionary(dict : [Int : String], containsValue value : String)->Bool{
let contains = dict.contains { (_,v) -> Bool in
return v == value
}
return contains
}
let c = dictionary(dic, containsValue: "c") // false
let a = dictionary(dic, containsValue: "a") // true
通用:
extension Dictionary{
func containsValue<T : Equatable>(value : T)->Bool{
let contains = self.contains { (k, v) -> Bool in
if let v = v as? T where v == value{
return true
}
return false
}
return contains
}
}
我已经针对 dictionary.values.contains()
测试了这个函数,它大约快两倍。
如果你想检查是否已经包含一个值,这将是这样的:
if !yourDictionary.values.contains("Zero") {
yourDictionary[newItemKey] = newItemValue; //addNewItem
}
else {
print("this value already exists");
}
如果你想检查 key 是否存在,这一个:
- 您得到要添加到您的词典中的项目。
- 检查项目的密钥是否已经存在
如果没有,请附加该项目或启用该按钮。
//1
let newItemKey = 0
let newItemValue = "Zero"
//2
let keyExists = yourDictionary[newItemKey] != nil
//3
if !keyExists {
yourDictionary[newItemKey] = newItemValue; //addNewItem
}
else {
print("This key already exists");
}
字典 getter returns 一个 optional 值。
let dictionary = ["ben": "says hi"]
let containsAlpha = dictionary["alpha"] != nil
let containsBen = dictionary["ben"] != nil
只是简单的任务。我有一个字典 var types = [Int : String]()
,它初始化时像一个空的,在一些用户操作后它填充了数据。根据本词典中的空虚或某些特定数据,我 enable/disable UI 中的一个按钮。
检查是否为空很容易,但是如何检查字典是否包含某个值呢?
编译器建议我使用谓词占位符:
types.contains(predicate: ((Int, String)) throws -> Bool>)
由于您只想检查给定的 值 是否存在,您可以对字典的 values
属性应用 contains
方法(给定本机 Swift 字典),例如
var types: [Int : String] = [1: "foo", 2: "bar"]
print(types.values.contains("foo")) // true
如values
属性貌似会产生开销(我自己没有验证过)w.r.t。只需根据每个 Dictionary
元素的键值元组中的值条目直接检查 contains
谓词。因为 Swift 很快,所以这应该不是问题,但是,除非你正在使用一个巨大的字典。无论如何,如果您想避免使用 values
属性,您可以查看上述答案中给出的替代方案,或者使用另一个替代方案(Dictionary
扩展名)作为如下:
extension Dictionary where Value: Equatable {
func containsValue(value : Value) -> Bool {
return self.contains { [=11=].1 == value }
}
}
types.containsValue("foo") // true
types.containsValue("baz") // false
我写了一个在字典上使用 contains
方法的函数。
您的具体情况:
let dic : [Int : String] = [1 : "a", 2 : "b"]
func dictionary(dict : [Int : String], containsValue value : String)->Bool{
let contains = dict.contains { (_,v) -> Bool in
return v == value
}
return contains
}
let c = dictionary(dic, containsValue: "c") // false
let a = dictionary(dic, containsValue: "a") // true
通用:
extension Dictionary{
func containsValue<T : Equatable>(value : T)->Bool{
let contains = self.contains { (k, v) -> Bool in
if let v = v as? T where v == value{
return true
}
return false
}
return contains
}
}
我已经针对 dictionary.values.contains()
测试了这个函数,它大约快两倍。
如果你想检查是否已经包含一个值,这将是这样的:
if !yourDictionary.values.contains("Zero") {
yourDictionary[newItemKey] = newItemValue; //addNewItem
}
else {
print("this value already exists");
}
如果你想检查 key 是否存在,这一个:
- 您得到要添加到您的词典中的项目。
- 检查项目的密钥是否已经存在
如果没有,请附加该项目或启用该按钮。
//1 let newItemKey = 0 let newItemValue = "Zero" //2 let keyExists = yourDictionary[newItemKey] != nil //3 if !keyExists { yourDictionary[newItemKey] = newItemValue; //addNewItem } else { print("This key already exists"); }
字典 getter returns 一个 optional 值。
let dictionary = ["ben": "says hi"]
let containsAlpha = dictionary["alpha"] != nil
let containsBen = dictionary["ben"] != nil