0

this code works

extension Character {
    func isVowel() -> Bool {
        switch self {
        case "a", "e", "i", "o", "u", "A", "E", "I", "O", "U":
            return true
        default:
            return false
        }
    }
}

but when I use an if statement it gives "cannot convert type "String" to Bool

extension Character {

    func isVowel() -> Bool {
        if "a", "e", "i", "o", "u" {
            return true
        } else {
            return false
        }
    }

}//does not work

I have tried adding "self" and self.asciiValue

how to properly convert the switch statement into if statement? sorry, still a beginner lol

  • This has nothing to do with “self not working.” `if` cannot be used with pattern matching and *multiple cases.* As explained in the duplicate, there are better solutions (such as `contains()` ) – Martin R Jun 09 '19 at 14:17

1 Answers1

0

The if statement should look like this:

if self == "a" || self == "e" || self == "i" || self == "o" || self == "u" {
  return true
} else {
  return false
}

Another way to do this would be:

func isVowel() -> Bool {
  return ["a", "e", "i", "o", "u"].contains(self)

  // or even shorter (by Leo Dabus):
  return "aeiou".contains(self)
}
Rengers
  • 14,911
  • 1
  • 36
  • 54