Skip to main content
Add use case as per request
Source Link
doovers
  • 8.7k
  • 10
  • 46
  • 73

Given a non-optional value, how can I match against an optional in a switch statement?

For example:

let test = "some string"

let x: String? = nil
let y: String? = "some string"
let z: String? = "another string"

switch test {
case x:
    print(x)
case y:
    print(y)
case z:
    print(z)
default: break
}

results in:

Expression pattern of type 'String?' cannot match values of type 'String'

for each case...

I've read the swift docs on patterns and switch but I can't seem to find a way of making this work.

I know I can work around this, but there must be a way of making this work...

Edit

As requested, here is my actual use case. Note all text fields are optional...

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // TODO: Replace with switch if possible
        
//        switch textField {
//        case nameTextField:
//            usernameTextField?.becomeFirstResponder()
//        case usernameTextField:
//            dateOfBirthTextField?.becomeFirstResponder()
//        case dateOfBirthTextField:
//            phoneTextField?.becomeFirstResponder()
//        case phoneTextField:
//            phoneTextField?.resignFirstResponder()
//        default: break
//        }
        
        if textField == nameTextField {
            usernameTextField?.becomeFirstResponder()
        } else if textField == usernameTextField {
            dateOfBirthTextField?.becomeFirstResponder()
        } else if textField == dateOfBirthTextField {
            phoneTextField?.becomeFirstResponder()
        } else if textField == phoneTextField {
            phoneTextField?.resignFirstResponder()
        }
        return false
    }

Given a non-optional value, how can I match against an optional in a switch statement?

For example:

let test = "some string"

let x: String? = nil
let y: String? = "some string"
let z: String? = "another string"

switch test {
case x:
    print(x)
case y:
    print(y)
case z:
    print(z)
default: break
}

results in:

Expression pattern of type 'String?' cannot match values of type 'String'

for each case...

I've read the swift docs on patterns and switch but I can't seem to find a way of making this work.

I know I can work around this, but there must be a way of making this work...

Given a non-optional value, how can I match against an optional in a switch statement?

For example:

let test = "some string"

let x: String? = nil
let y: String? = "some string"
let z: String? = "another string"

switch test {
case x:
    print(x)
case y:
    print(y)
case z:
    print(z)
default: break
}

results in:

Expression pattern of type 'String?' cannot match values of type 'String'

for each case...

I've read the swift docs on patterns and switch but I can't seem to find a way of making this work.

I know I can work around this, but there must be a way of making this work...

Edit

As requested, here is my actual use case. Note all text fields are optional...

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // TODO: Replace with switch if possible
        
//        switch textField {
//        case nameTextField:
//            usernameTextField?.becomeFirstResponder()
//        case usernameTextField:
//            dateOfBirthTextField?.becomeFirstResponder()
//        case dateOfBirthTextField:
//            phoneTextField?.becomeFirstResponder()
//        case phoneTextField:
//            phoneTextField?.resignFirstResponder()
//        default: break
//        }
        
        if textField == nameTextField {
            usernameTextField?.becomeFirstResponder()
        } else if textField == usernameTextField {
            dateOfBirthTextField?.becomeFirstResponder()
        } else if textField == dateOfBirthTextField {
            phoneTextField?.becomeFirstResponder()
        } else if textField == phoneTextField {
            phoneTextField?.resignFirstResponder()
        }
        return false
    }
Source Link
doovers
  • 8.7k
  • 10
  • 46
  • 73

Match optional in switch statement

Given a non-optional value, how can I match against an optional in a switch statement?

For example:

let test = "some string"

let x: String? = nil
let y: String? = "some string"
let z: String? = "another string"

switch test {
case x:
    print(x)
case y:
    print(y)
case z:
    print(z)
default: break
}

results in:

Expression pattern of type 'String?' cannot match values of type 'String'

for each case...

I've read the swift docs on patterns and switch but I can't seem to find a way of making this work.

I know I can work around this, but there must be a way of making this work...