Added nil-coalescing operator

This commit is contained in:
Kyle Rokita 2016-11-11 16:12:13 -08:00 committed by GitHub
parent d70ad72219
commit a19efad7fc

View File

@ -104,6 +104,11 @@ if let someOptionalStringConstant = someOptionalString {
} }
} }
// The nil-coalescing operator ?? unwraps an optional if it contains a non-nil value, or returns a default value.
var someOptionalString: String?
let someString = someOptionalString ?? "abc"
print(someString) // abc
// Swift has support for storing a value of any type. // Swift has support for storing a value of any type.
// For that purposes there is two keywords: `Any` and `AnyObject` // For that purposes there is two keywords: `Any` and `AnyObject`
// `AnyObject` == `id` from Objective-C // `AnyObject` == `id` from Objective-C