//
|
// DictionaryExtensition.swift
|
// ExhibitionProject
|
//
|
// Created by alvin_y on 2019/1/25.
|
// Copyright © 2019 yang-wang. All rights reserved.
|
//
|
|
import Foundation
|
|
func += <K,V> ( left: inout Dictionary<K,V>, right: Dictionary<K,V>?) {
|
guard let right = right else { return }
|
right.forEach { key, value in
|
left.updateValue(value, forKey: key)
|
}
|
}
|
|
extension Dictionary {
|
|
mutating func append(dict: Dictionary) {
|
dict.forEach { (key, value) in
|
self.updateValue(value, forKey: key)
|
}
|
}
|
}
|
|
extension Dictionary{
|
func dictionaryToJson() -> String? {
|
do {
|
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
|
return String.init(data: jsonData, encoding: .utf8) ?? nil
|
} catch {
|
print(error.localizedDescription)
|
return nil
|
}
|
}
|
}
|
extension Array {
|
|
// 去重
|
func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
|
var result = [Element]()
|
for value in self {
|
let key = filter(value)
|
if !result.map({filter($0)}).contains(key) {
|
result.append(value)
|
}
|
}
|
return result
|
}
|
}
|