May 1, 2024, 2:32 p.m. | BC

DEV Community dev.to




Swift



  • functions with throw Exceptions



import Cocoa

enum PasswordError: Error, LocalizedError {
case TooShort
case Obvious

var errorDescription: String? {
switch self {
case .TooShort:
return NSLocalizedString("Password is too short", comment: "Too short")
case .Obvious:
return NSLocalizedString("Password is too obvious", comment: "Obvious")
}
}
}

// although defined with `throws`,
// this function doesn't have to throw an exception
func checkPassword(_ pwd: String) throws -> Bool {
if pwd.count < 6 {
throw PasswordError.TooShort
}
if pwd == "123456" { …

case defined error function functions import learn password return swift swiftui switch var

Information Security Engineers

@ D. E. Shaw Research | New York City

Technology Security Analyst

@ Halton Region | Oakville, Ontario, Canada

Senior Cyber Security Analyst

@ Valley Water | San Jose, CA

Sr. Application Security Engineer

@ CyberCube | Tallinn

Security Incident Response Analyst

@ Oracle | KITCHENER, ON, Canada

Senior Security Engineer

@ Minitab | Americas Remote