Handful of functions, operators and properties that will help you work with Optional types.
Call require() to require Optional values to be non-nil, or throws fatalError with optionally given hint for debugging purposes:
let gitURL: URL? = URL(string: "https://github.com/Smitters/OptionalExtensions")
let request = URLRequest(url: gitURL.require(hint: "Invalid URL"))Call unwrap(default: value) to return the contained value or a default:
let daysInMonth: Int? = nil
let daysCount = daysInMonth.unwrap(default: 31) // returns 31Property isNone returns true if the Optional is nil:
let x: String? = nil
let r = x.isNone // r == trueProperty isSome returns true if the Optional is .some:
let x: String? = "foo"
let r = x.isSome // r == trueProperty stringRepresentation ensures that the text you set never ever includes that annoying additional “Optional(…)”:
let x: String? = "String"
let y: Int? = 31
let z: String? = nil
print("\(x) \(y) \(z)") // Optional("String") Optional(31) nil
print("\(x.stringRepresentation) \(y.stringRepresentation) \(z.stringRepresentation)") // String 31Swift 3 Removes Optional Comparison Operators, but you can achieve same behaviour adding ? symbol to Swift's standart comparison operator:
let x: Int? = 43
let y: Int? = 5
let z: Int? = nil
x >=? y // true
x >? z // true
z >? x // false
y <? x // true
x <=? 43 // true
x <=? y //false
let array: [Int?] = [2, 4, nil, 1, 5, nil, 3]
let sortedArray = array.sorted(by: >?) // [5, 4, 3, 2, 1, nil, nil]CocoaPods:
Add pod "ExtOptional" to your Podfile.
Manual:
Clone the repo and drag the file OptionalExtensions.swift into your Xcode project.
- Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
 - Open a PR if you want to make some change to 
ExtOptional. - Contact @smetankin93 on Twitter for discussions, news & announcements about new pods.
 


