Create a gist now

Instantly share code, notes, and snippets.

Playground examples of how one may deal with checking for multiple entries in a dictionary that may or may not be there.
import Cocoa
let example1: [String: NSNumber] = ["a":10, "b":20, "c":30]
let example2: [String: NSNumber] = ["a":10, "c":30]
var ex = example1;
if let a = ex["a"]?.integerValue {
if let b = ex["b"]?.integerValue {
if let c = ex["c"]?.integerValue {
"\(a) \(b) \(c)"
}
}
}
var a = ex["a"]?.integerValue, b = ex["b"]?.integerValue, c = ex["c"]?.integerValue
if (a != nil && b != nil && c != nil) {
"\(a!) \(b!) \(c!)"
}
ex = example2;
if let a = ex["a"]?.integerValue {
if let b = ex["b"]?.integerValue {
if let c = ex["c"]?.integerValue {
"\(a) \(b) \(c)"
}
}
}
a = ex["a"]?.integerValue; b = ex["b"]?.integerValue; c = ex["c"]?.integerValue
if (a != nil && b != nil && c != nil) {
"\(a!) \(b!) \(c!)"
}
let sample: Int = (ex["a"]?.integerValue) ?? 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment