Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/reference/foundation/nsmutabledictionary\).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/reference/swift/dictionary\), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example:
let d1 = ["Apples": 20, "Oranges": 13]
let d2 = ["Oranges": 3, "Cherries": 9]
extension Dictionary {
func merged(with another: [Key: Value]) -> [Key: Value] {
var result = self
for entry in another {
result[entry.key] = entry.value
}
return result
}
}
let result = d1.merged(with: d2)
···
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/reference/foundation/nsmutabledictionary\).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/reference/swift/dictionary\), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
When you add a single entry to a dictionary with =, it simply replaces any existing value. I would expect adding multiple entries to do the same. (Which is what NSDictionary’s -addEntries: method does.)
Sure, there are different policies you could want, like ignoring duplicate keys or somehow combining the values, but replacing is a common default. It seems wrong not to have this method just because there’s more than one way it could work. (“Don’t let the best be the enemy of the good.”)
—Jens
···
On Nov 11, 2016, at 12:14 AM, Ray Fix via swift-users <swift-users@swift.org> wrote:
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide.
Nate Cook proposed merging dictionary initializers (with optional conflict resolution) to be added to the standard library:
SE-0100 Add sequence-based initializers and merge methods to Dictionary
https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md
Martin
···
On 11 Nov 2016, at 09:14, Ray Fix via swift-users <swift-users@swift.org> wrote:
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example:
let d1 = ["Apples": 20, "Oranges": 13]
let d2 = ["Oranges": 3, "Cherries": 9]extension Dictionary {
func merged(with another: [Key: Value]) -> [Key: Value] {
var result = self
for entry in another {
result[entry.key] = entry.value
}
return result
}
}let result = d1.merged(with: d2)
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/reference/foundation/nsmutabledictionary\).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/reference/swift/dictionary\), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy.
Then how NSMutableDictionary.addEntries() solve this issue?
I thought with Swift design, we could merge some compatible dictionaries simply by using + operator array.
–Mr Bee
Pada Jumat, 11 November 2016 15:14, Ray Fix <rayfix@gmail.com> menulis:
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example:
let d1 = ["Apples": 20, "Oranges": 13]let d2 = ["Oranges": 3, "Cherries": 9]
extension Dictionary { func merged(with another: [Key: Value]) -> [Key: Value] { var result = self for entry in another { result[entry.key] = entry.value } return result }}
let result = d1.merged(with: d2)
···
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/reference/foundation/nsmutabledictionary\).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/reference/swift/dictionary\), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
For example, you might want to merge values:
let d1 = ["Apples": 20, "Oranges": 13]
let d2 = ["Oranges": 3, "Cherries": 9]
let d3 = [d1, d2].reduce([String : Int]()) { acc, d in
var dx = acc
for entry in d {
let key = entry.key
guard dx[key] == nil else {
dx[key]! += entry.value
continue
}
dx[entry.key] = entry.value
}
return dx
}
print(d3) // ["Cherries": 9, "Apples": 20, "Oranges": 16]
···
On Fri, Nov 11, 2016 at 9:14 AM, Ray Fix via swift-users < swift-users@swift.org> wrote:
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know
what to do when keys collide. You could easily write such a thing and
decide your own policy. For example:let d1 = ["Apples": 20, "Oranges": 13]
let d2 = ["Oranges": 3, "Cherries": 9]extension Dictionary {
func merged(with another: [Key: Value]) -> [Key: Value] {
var result = self
for entry in another {
result[entry.key] = entry.value
}
return result
}
}let result = d1.merged(with: d2)
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users < > swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary
into another compatible dictionary. However, I couldn't find addEntries
function in the dictionary instance, like it was on NSMutableDictionary (
https://developer.apple.com/reference/foundation/nsmutabledictionary\).Does that mean that Swift standard library won't provide such similar
function for native Swift dictionary? Or is there any other way of doing
that natively? I mean using the built-in Swift's native dictionary function
(https://developer.apple.com/reference/swift/dictionary\), no need to
write a custom function, or bridging to NSMutableDictionary.Thank you.
Regards,
–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
SE-0100 Add sequence-based initializers and merge methods to Dictionary
Thank you for the information. It looks like a good proposal.
–Mr Bee
Pada Jumat, 11 November 2016 15:50, Martin R <martinr448@gmail.com> menulis:
Nate Cook proposed merging dictionary initializers (with optional conflict resolution) to be added to the standard library:
SE-0100 Add sequence-based initializers and merge methods to Dictionary
https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md
Martin
···
On 11 Nov 2016, at 09:14, Ray Fix via swift-users <swift-users@swift.org> wrote:
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example:
let d1 = ["Apples": 20, "Oranges": 13]
let d2 = ["Oranges": 3, "Cherries": 9]extension Dictionary {
func merged(with another: [Key: Value]) -> [Key: Value] {
var result = self
for entry in another {
result[entry.key] = entry.value
}
return result
}
}let result = d1.merged(with: d2)
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/reference/foundation/nsmutabledictionary\).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/reference/swift/dictionary\), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
Thank you for your answer, but as I've said, I'm looking for a ready to use solution right from the Dictionary data type. Not using custom function or extension, neither bridging with NSMutableDictionary. Actually, as I'm now learning Swift, I write my learning process into a Swift blog (but in my local language). I was surprised to find that dictionary doesn't have method for this simple operation. I was expecting it would be as easy as using + operator like in array data type.
–Mr Bee
    Pada Jumat, 11 November 2016 16:31, Michael Nisi <michael.nisi@gmail.com> menulis:
For example, you might want to merge values:
let d1 = ["Apples": 20, "Oranges": 13]let d2 = ["Oranges": 3, "Cherries": 9]
let d3 = [d1, d2].reduce([String : Int]()) { acc, d in var dx = acc for entry in d { let key = entry.key guard dx[key] == nil else { dx[key]! += entry.value continue } dx[entry.key] = entry.value } return dx}
print(d3) // ["Cherries": 9, "Apples": 20, "Oranges": 16]
···
On Fri, Nov 11, 2016 at 9:14 AM, Ray Fix via swift-users <swift-users@swift.org> wrote:
Hi Mr Bee,
The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example:
let d1 = ["Apples": 20, "Oranges": 13]let d2 = ["Oranges": 3, "Cherries": 9]
extension Dictionary { func merged(with another: [Key: Value]) -> [Key: Value] { var result = self for entry in another { result[entry.key] = entry.value } return result }}
let result = d1.merged(with: d2)
On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <swift-users@swift.org> wrote:
Hi,
I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (https://developer.apple.com/ reference/foundation/ nsmutabledictionary).
Does that mean that Swift standard library won't provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift's native dictionary function (https://developer.apple.com/ reference/swift/dictionary), no need to write a custom function, or bridging to NSMutableDictionary.
Thank you.
Regards,
–Mr Bee
______________________________ _________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/ mailman/listinfo/swift-users
______________________________ _________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/ mailman/listinfo/swift-users