Skip to content

Instantly share code, notes, and snippets.

@TuenTuenna
Last active February 10, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TuenTuenna/4f432ce4d9fc28488556ac9943aefd51 to your computer and use it in GitHub Desktop.
Save TuenTuenna/4f432ce4d9fc28488556ac9943aefd51 to your computer and use it in GitHub Desktop.
Optiona Tupple + Unwrapping

정대리 스위프트 기초문법 - 81일차 내용 참고

https://spangle-wedelia-2dc.notion.site/Swift-Tip-of-the-day-c428bfd990674bcfa2a4973e5d08c4eb

참고링크: https://stackoverflow.com/questions/27991378/swift-optional-binding-with-tuples

//if case let (user?, pass?) = (user, pass) { }

extension Optional {
    init<T, U>(_ optionalTuple: (T?, U?)) where Wrapped == (T, U) {
        
//        switch optionalTuple{
//        case let (.some(t?), .some(u?)):
//            self = (t, u)
//        default:
//            self = nil
//        }
        
        switch optionalTuple{
            case (let t?, let u?):
            self = (t, u)
        default:
            self = nil
        }
    }
}

사용예

Optional(( $0 as? String, $1 as? Int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment