unwrapping multiple optionals in if statement

前端 未结 8 615
鱼传尺愫
鱼传尺愫 2020-11-28 07:38

I want to unwrap two optionals in one if statement, but the compiler complaints about an expected expression after operator at the password constant. What could be the reaso

8条回答
  •  长情又很酷
    2020-11-28 08:09

    Based on @Joel's answer, I've created a helper method.

    func unwrap(a:T?, b:U?, handler:((T, U) -> ())?) -> Bool {
        switch (a, b) {
        case let (.Some(a), .Some(b)):
            if handler != nil {
                handler!(a, b)
            }
            return true
        default:
            return false
        }
    }
    

    // Usage

    unwrap(a, b) {
        println("\($0), \($1)")
    }
    

提交回复
热议问题