Practice: Bank Account
Combine struct, mutating and errors.
Example
enum BankError: Error { case invalid }
struct Account {
private(set) var balance = 0.0
mutating func deposit(_ n: Double) throws {
guard n > 0 else { throw BankError.invalid }
balance += n
}
}
var a = Account()
try a.deposit(50)
print(a.balance)Exercise
Validate first.
n > 0 else { throw E }Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.