Reputation: 621
I have a dictionary of the following type
[String : [String]]
In my app, during runtime I make a request to update a var containing a dictionary of the above type. I also store the same type of dictionary in CoreData.
My question is what would be the best method to compare two of these dictionaries for equality? I was thinking that a hashing function would be best for this but not sure how to approach it
Upvotes: 2
Views: 651
Reputation: 725
I believe this functionality is native to swift 4:
https://developer.apple.com/documentation/swift/dictionary/2430767
A function like this will work considering the compiler will check the type for you and guarantee a key:
func compare(left:[String:[String]], right: [String:[String]]) -> Bool {
return left.keys == right.keys && left[left.keys.first!]! == right[right.keys.first!]!
}
But if you want to loop through it for some reason, a good way would be declaratively.
Upvotes: 2
Reputation: 3232
I tried this example:
var dict1: [String: [String]]!
var dict2: [String: [String]]!
func compareDictionaries(){
if dict1 == dict2{
print("equals")
}
}
Upvotes: 1