unwrapping multiple optionals in if statement

前端 未结 8 599
鱼传尺愫
鱼传尺愫 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 07:50

    How about wrapping the optionals in a tuple and using switch to pattern match?

    switch (self.emailField?.text, self.passwordField?.text) {
    case let (.Some(email), .Some(password)):
        // unwrapped 'email' and 'password' strings available here
    default:
        break
    }
    

    It's definitely a bit noisier, but at least it could also be combined with a where clause as well.

提交回复
热议问题