Swift - 学用 字典 Dictionary

40 篇文章 0 订阅
17 篇文章 0 订阅

1. 字典映射生成新的集合

1). 方法: mapValues, compactMapValues, 返回新的字典集合

这两个方法会遍历字典,可以在block中得到value的,没有得到key,
然后在block中返回新的value
区别:mapValues不可以返回nil。compactMapValues可以返回nil,会删除该键值对。

var dic = [String: Any]()
dic["key1"] = "aaaa1"
dic["key2"] = "aaaa2"
dic["key3"] = "aaaa3"
dic["key4"] = "aaaa4"
dic["key5"] = "aaaa5"
dic["key6"] = "aaaa6"
dic["key7"] = "aaaa7"

let result = dic.mapValues { (obj) -> Any/*T*/ in
    print(obj)
    return obj as! String + ".#"
}
print(result)
/*
打印:
aaaa5
aaaa1
aaaa3
aaaa2
aaaa6
aaaa4
aaaa7

result: 
["key5": "aaaa5.#", "key2": "aaaa2.#", "key6": "aaaa6.#", "key4": "aaaa4.#", "key3": "aaaa3.#", "key7": "aaaa7.#", "key1": "aaaa1.#"]
*/

//------------------------------------------
let result2 = dic.compactMapValues { (obj) -> String?/*T?*/ in
    print(obj)
    
    if obj as! String == "aaaa1" || obj as! String == "aaaa2" {
        return obj as! String + ".###"
    } else {
        return nil  //nil 会清除元素
    }
}
/*
打印obj:
aaaa7
aaaa5
aaaa2
aaaa6
aaaa4
aaaa3
aaaa1

result2:
["key2": "aaaa2.###", "key1": "aaaa1.###"]
*/

2). 方法:map, flatMap, compactMap

三个方法都会遍历字典,并且遍历过程可以在block中取得key和value, 返回的结果是数组

‘flatMap’ is deprecated: Please use compactMap(_: ) for the case where closure returns an optional value, 过期

var dic = [String: Any]()
dic["key1"] = "aaaa1"
dic["key2"] = "aaaa2"
dic["key3"] = "aaaa3"
dic["key4"] = "aaaa4"
dic["key5"] = "aaaa5"
dic["key6"] = "aaaa6"
dic["key7"] = "aaaa7"

//============= 可以这么写:
//返回数组,元素是泛型 ElementOfResult?
let result = dic.compactMap { (key, value) -> Any?/*ElementOfResult?*/ in
    //TODO:
    return nil
}

//--------------------------

let result = dic.map { (element) -> Any/*T*/ in
    print(element)
    let (key, value) = element
    return element
}
print(result) //返回值是数组类型
/*
打印element:
(key: "key3", value: "aaaa3")
(key: "key6", value: "aaaa6")
(key: "key4", value: "aaaa4")
(key: "key5", value: "aaaa5")
(key: "key1", value: "aaaa1")
(key: "key2", value: "aaaa2")
(key: "key7", value: "aaaa7")

result:
[(key: "key3", value: "aaaa3"), (key: "key6", value: "aaaa6"), (key: "key4", value: "aaaa4"), (key: "key5", value: "aaaa5"), (key: "key1", value: "aaaa1"), (key: "key2", value: "aaaa2"), (key: "key7", value: "aaaa7")]
*/

//-------------------------------------
let result2 = dic.flatMap { (element) -> Any /*Sequence*/ in
    print(element)
    let (key, value) = (element)
    return element
    
}
print(result2)

/*
打印element:
(key: "key3", value: "aaaa3")
(key: "key4", value: "aaaa4")
(key: "key6", value: "aaaa6")
(key: "key5", value: "aaaa5")
(key: "key7", value: "aaaa7")
(key: "key2", value: "aaaa2")
(key: "key1", value: "aaaa1")

result2:
[(key: "key3", value: "aaaa3"), (key: "key4", value: "aaaa4"), (key: "key6", value: "aaaa6"), (key: "key5", value: "aaaa5"), (key: "key7", value: "aaaa7"), (key: "key2", value: "aaaa2"), (key: "key1", value: "aaaa1")]
*/

//---------------------------

let result3 = dic.compactMap { (element) -> Any? in
    print(element)
    let (key, value) = (element)
    
    if key == "key1" || key == "key2" || key == "key3" || key == "key4" {
        return nil  //
    }
    return element
}
print(result3)

/*
打印element:
(key: "key3", value: "aaaa3")
(key: "key4", value: "aaaa4")
(key: "key6", value: "aaaa6")
(key: "key5", value: "aaaa5")
(key: "key7", value: "aaaa7")
(key: "key2", value: "aaaa2")
(key: "key1", value: "aaaa1")

result2:
[(key: "key6", value: "aaaa6"), (key: "key5", value: "aaaa5"), (key: "key7", value: "aaaa7")]
*/


2. 过滤字典

方法: filter

var dic = [String: Any]()
dic["key1"] = "aaaa1"
dic["key2"] = "aaaa2"
dic["key3"] = "aaaa3"
dic["key4"] = "aaaa4"
dic["key5"] = "aaaa5"
dic["key6"] = "aaaa6"
dic["key7"] = "aaaa7"

//
let result2 = dic.filter { (key, value) -> Bool in
    if key == "key1" || key == "key2" || key == "key3" || key == "key4" {
           return false  //
       }
       return true
}

//另一种写法
let result = dic.filter { (element) -> Bool in
    print(element)
    let (key, value) = (element)
    if key == "key1" || key == "key2" || key == "key3" || key == "key4" {
        return false  // 会过滤掉
    }
    return true
}
print(result) //结果是字典类型

/*
打印element:
(key: "key5", value: "aaaa5")
(key: "key6", value: "aaaa6")
(key: "key3", value: "aaaa3")
(key: "key7", value: "aaaa7")
(key: "key4", value: "aaaa4")
(key: "key2", value: "aaaa2")
(key: "key1", value: "aaaa1")

result:
["key5": "aaaa5", "key6": "aaaa6", "key7": "aaaa7"]
*/

3. 合并字典

swift api: merge

var dic1: [String: Float] = ["k1": 0.1,"k2": 0.2,"k3": 0.3]
var dic2: [String: Float] = ["k1": 0.111,"k2": 0.222,"k3": 0.333,"k4": 0.44,"k5": 0.55]

dic1.merge(dic2) { (current, new) -> Float in
    
    print("value in dic1: \(current), dic2: \(new)")
    return current
}

print(dic1)
print(dic2)
/*
打印:
value in dic1: 0.1, dic2: 0.111
value in dic1: 0.2, dic2: 0.222
value in dic1: 0.3, dic2: 0.333
["k3": 0.3, "k2": 0.2, "k4": 0.44, "k5": 0.55, "k1": 0.1]
["k4": 0.44, "k5": 0.55, "k1": 0.111, "k2": 0.222, "k3": 0.333]
*/

//dic1.merging(dic2) { (current, new) -> Float in
//    print("value in dic1: \(current), dic2: \(new)")
//    return value1
//}
//
//print(dic1)
//print(dic2)

扩展

获取指定的key-values

extension Dictionary {
    func keyValues(with keys:[Key]) -> [Key: Value] {
        let result = self.filter { (key, _) -> Bool in
            return keys.contains(key)
        }
        return result
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值