-
Notifications
You must be signed in to change notification settings - Fork 0
when function
Sergey Makeev edited this page Sep 28, 2019
·
4 revisions
func when<Type, ResultType> (_ source: Type, handler: (Type) -> ResultType) -> ResultTypesource is a value of Type to be transformed to ResultType.
handler is a block - how to cast.
let c = when(number) {
//Here could be any culculations
return $0 >= 10 ? "Big number" : "Small number"
}In 2.0.0 there were added several more functions.
Global:
func with<Type>(_ source: Type, handler: (Type) -> Void)
extension for Optionals:
func takeWhen(_ handler: (Wrapped) -> Bool) -> Wrapped?
func `let`<Result>(_ handler: (Wrapped) -> Result?) -> Result?
func apply(_ handler: (Wrapped) -> Void) -> Wrapped?
func run(_ handler: (Wrapped) -> Void)with is similar to when but does not return anything.
This is just a way to get shorter access to multi level embedded properties like "ClassA.ClassB.ClassC..."
takeWhen will return nil if Optinal is nil or its condition returns false.
let returns Optinal or nil. Is similar to map. It could return anything, but it will be Optinal.
apply always returns Optinal itself. It could be used in chain optional.apply{...}.apply...
run does not return anything. Is similar to map without return