I'm doing something wrong here, but I'm not sure quite what I'm doing wrong.
For some reason my switch case always goes to the default case instead of the proper case.
I added the default because before it was saying that the switch needed it.
Here is my function call with the password passed into it
do { try self.validate(password: password) } catch { print("Error Label", error.localizedDescription) // errorLabel.text = error.localizedDescription } This is the function itself:
func validate(password: String) throws { guard password.count == 0 else { print("too short") HapticsManager.shared.vibrate(for: .error) throw ValidationError.noPass } guard password.count > 3 else { print("too short") HapticsManager.shared.vibrate(for: .error) throw ValidationError.tooShort } guard password.count < 15 else { print("too long") HapticsManager.shared.vibrate(for: .error) throw ValidationError.tooLong } for character in password { guard character.isLetter else { HapticsManager.shared.vibrate(for: .error) throw ValidationError.invalidCharacterFound(character) } } } This is my enum:
enum ValidationError: Error { case noPass case tooShort case tooLong case invalidCharacterFound(Character) } And this is my enum extension and where the problem occurs:
extension ValidationError: LocalizedError { var errorDescription: String? { print("helloooooo") switch self { case .tooShort: print("TOO SHORT") return NSLocalizedString( "Your username needs to be at least 4 characters long", comment: "" ) case .tooLong: return NSLocalizedString( "Your username can't be longer than 14 characters", comment: "" ) case .invalidCharacterFound(let character): let format = NSLocalizedString( "Your username can't contain the character '%@'", comment: "" ) return String(format: format, String(character)) default: return "There was an error." } } } If I type a password that is too short I expect it to go into the tooShort case but it goes into the default case every time.
Any help would be appreciated.