Created
October 26, 2014 22:26
-
-
Save danschultz/da3cfca0b4acd82e0a00 to your computer and use it in GitHub Desktop.
A couple helpers for working with optionals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func ifOptionalHasValue<T>(optional: T?, then: (T) -> Void) { | |
ifOptionalHasValue(optional, { (value) -> Void in | |
then(value) | |
}) { () -> Void in | |
// do nothing | |
} | |
} | |
func ifOptionalHasValue<T>(optional: T?, then: (T) -> Void, orElse: (() -> Void)?) { | |
if let optionalValue = optional { | |
then(optionalValue) | |
} else { | |
if let orElseBlock = orElse { | |
orElseBlock() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment