I am currently using the following (clumsy) pieces of code for determining if a (non-empty) Swift dictionary contains a given key and for obtaining one (any) value from the same dictionary.
How can one put this more elegantly in Swift?
// excerpt from method that determines if dict contains key
if let _ = dict[key] {
return true
}
else {
return false
}
// excerpt from method that obtains first value from dict
for (_, value) in dict {
return value
}
indexForKey
if you feel it is clearer and more explicit; https://mcmap.net/q/94194/-check-if-key-exists-in-dictionary-of-type-type-type – IeycityName:String = dict["city"] ?? ""
The?? ""
here means basically "if there's no such key, return a blank". – IeyDictionary.keys.contains()
is O(1) for practical purposes (O(n) is worst case.). And more readable than the alternatives here, and handles optional values correctly. See here. – CropperdeleteRule
does not work as it should. I disabled it and manually saved the context every time I added or edited any of the model objects and somehow thedeleteRule
does not apply correctly. Also the views does not update correctly. If I enable it, the view updates correctly (but I still have an issue) and thedeleteRule
apply as expected. – Infra