Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Created February 5, 2015 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STAR-ZERO/d5e1c6dfbd825d98cecb to your computer and use it in GitHub Desktop.
Save STAR-ZERO/d5e1c6dfbd825d98cecb to your computer and use it in GitHub Desktop.
複数のOptionalをunwrap
import Foundation
func unwrap2<T1, T2>(a: T1?, b: T2?) -> (T1, T2)? {
switch (a, b) {
case (let .Some(a), let .Some(b)):
return (a, b)
default:
return nil
}
}
func test(a: String?, b: String?) {
if let (_a, _b) = unwrap2(a, b) {
println("\(_a) : \(_b)")
} else {
println("nil")
}
}
test("a", "b") // a : b
test("a", nil) // nil
test(nil, "b") // nil
test(nil, nil) // nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment