Uploaded image for project: 'Swift'
  1. Swift
  2. SR-2022

Better fixit for type checking in switch statements

    XMLWordPrintable

    Details

      Description

      Switch myVariable {
          case let intValue as? Int: print("Hello, I'm an int: \(intValue)")
          case let stringValue as? String: print("Hello, I'm a string: \(stringValue)")
          default: print("Some other value...")
      }
      

      Gives you an error on the casting lines saying "Invalid pattern". It would be better if it told you to make the casts non-conditional, I.e.:

      Switch myVariable {
          case let intValue *as Int*: print("Hello, I'm an int: \(intValue)")
          case let stringValue *as String*: print("Hello, I'm a string: \(stringValue)")
          default: print("Some other value...")
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            kawa Karl
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated: