I was thinking the following syntax:
extension Dictionary where Key:String,Value:String{
func addIfNew(key:String,value:String){
print("new item added: \(key) with value: \(value)")
}
}
For example: the function would log any new added data
This should be enough ...
protocol P {}
extension String: P {}
extension Dictionary where Key:P, Value:P {
func addIfNew(key:String, value:String){
print("new item added: \(key) with value: \(value)")
}
}
let d:[String:String] = [:]
don't make other types conforming to P and you are fine
in Swift3
extension Dictionary where Key == String, Value == String {
func addIfNew(key:String, value:String){
print("new item added: \(key) with value: \(value)")
}
}
let d:[String:String] = [:]
d.addIfNew(key: "a", value: "A")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With