Tutorial by Examples

Functions in Swift may return values, throw errors, or both: func reticulateSplines() // no return value and no error func reticulateSplines() -> Int // always returns a value func reticulateSplines() throws // no return value, but may throw an error func reticulat...
Let's create our own error type for this example. 2.2 enum CustomError: ErrorType { case SomeError case AnotherError } func throwing() throws { throw CustomError.SomeError } 3.0 enum CustomError: Error { case someError case anotherError } func throwing() thr...
class Plane { enum Emergency: ErrorType { case NoFuel case EngineFailure(reason: String) case DamagedWing } var fuelInKilograms: Int //... init and other methods not shown func fly() throws { // ... if fuelInKilograms ...
The creators of Swift have put a lot of attention into making the language expressive and error handling is exactly that, expressive. If you try to invoke a function that can throw an error, the function call needs to be preceded by the try keyword. The try keyword isn't magical. All it does, is mak...
Create enum of custom errors enum RegistrationError: Error { case invalidEmail case invalidPassword case invalidPhoneNumber } Create extension of RegistrationError to handle the Localized description. extension RegistrationError: LocalizedError { public var errorDescription...

Page 1 of 1