我在这里做错了什么,但我不是很确定我做错了什么。
由于某些原因,我的切换用例总是使用默认用例,而不是正确的用例。
我添加了默认值,因为之前它说交换机需要它。
下面是我的函数调用,其中传入了密码
do {
try self.validate(password: password)
} catch {
print("Error Label", error.localizedDescription)
// errorLabel.text = error.localizedDescription
}这是函数本身:
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)
}
}
}这是我的枚举:
enum ValidationError: Error {
case noPass
case tooShort
case tooLong
case invalidCharacterFound(Character)
}这是我的枚举扩展,也是出现问题的地方:
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."
}
}
}如果我输入的密码太短,我希望它会进入tooShort大小写,但每次都会进入默认大小写。
任何帮助都将不胜感激。
发布于 2021-07-21 04:48:18
问题是这一行:
guard password.count == 0 else {计数从不为零。所以我们总是落入这个else子句!这就是.noPass投掷。
throw ValidationError.noPass但是您的交换机无法处理这种情况,因此它运行默认设置。
https://stackoverflow.com/questions/68461061
复制相似问题