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

Security Operations Manager-West Coast

@ The Walt Disney Company | USA - CA - 2500 Broadway Street

Vulnerability Analyst - Remote (WFH)

@ Cognitive Medical Systems | Phoenix, AZ, US | Oak Ridge, TN, US | Austin, TX, US | Oregon, US | Austin, TX, US

Senior Mainframe Security Administrator

@ Danske Bank | Copenhagen V, Denmark