This extesnions allows you to write more readable code and not to use nil coalliasing operator "??" in order to provide default value for a common Swift types.
Examples:
- String/Array/Set/Dictionary
let someOptionalString: String? = nil
// instead of this
let someString: String = someOptionalString ?? ""
// we will use this
let someString: String = someOptionalString.orEmpty
- Int/Double/Float/CGFloat...
let someOptionalNumber: Int? = nil
// instead of this
let someNumber: Int = someOptionalNumber ?? 0
// we will use this
let someNumber: Int = someOptionalNumber.orZero
- Date
let someOptinonalDate: Date? = nil
// instead of this
let someDate: Date = someOptinonalDate ?? Date()
// we will use this
let someDate: Date = someOptinonalDate.orToday
- Bool
let someOptionalBool: Bool? = nil
// instead of this
let someBool: Bool = someOptionalBool ?? false
// we will use this
let someBool: Bool = someOptionalBool.orFalse
For optional chaining, it would look like this
let someNumber = (someNonOptionalValue.someOptionalValue?.someNonOptionalNumber).orZero