SwiftBank

I am getting an error " SwiftBank.swift:51:3: error: use of unresolved identifier ‘didSet’
didSet {"

Can anyone explain this to me? Do I need to include a getter?

struct SwiftBank{
  private var balance: Double = 0
  static let depositBonusRate = 0.01
  private let password: String
  init(password: String, initialDeposit: Double){
    self.password = password
    makeDeposit(ofAmount: initialDeposit)
  }
private func isValid(_ enteredPassword: String) -> Bool {
  if enteredPassword == password{
    return true
  }
  else{
    return false
  }
  
}
private func finalDepositWithBonus(fromInitialDeposit deposit: Double) -> Double{
  return deposit + (deposit * SwiftBank.depositBonusRate)
}

mutating func makeDeposit(ofAmount depositAmount: Double){
  let depositWithBonus = finalDepositWithBonus(fromInitialDeposit: depositAmount)
  self.balance += depositWithBonus
  print("Making a deposit of $\(depositAmount) with a bonus rate. The final amount depositited is \(depositWithBonus).")
}

func displayBalance(usingPassword password: String){
 if isValid(password){
  print("Your current balance is $\(balance)")
  }
  else{
    print("Error: Invalid password. Cannot retrieve balance.")
    return
  }
}

mutating func makeWithdrawal(ofAmount withdrawalAmount: Double, usingPassword password: String){
  if isValid(password){
    self.balance -= withdrawalAmount
    print("Making a $\(withdrawalAmount) withdrawal.")
  }
  else{
    print("Error: Invalid password. Cannot make withdrawal.")
    return
  }
}
private func displayLowBalanceMessage(){
  didSet {
    if balance < 100{
      print("Alert: Your balance is under $100")
    }
  }
  
}

}

let myAccount = SwiftBank(password: "BoilerUp", initialDeposit: 500)

What is didSet? You can think of an identifier as a technical term for a variable/function/method name. This means the compiler or interpreter is scanning your code, sees didSet, and doesn’t have it defined in its symbol table so it doesn’t know what to do, hence the error.

private func displayLowBalanceMessage(){
  didSet {
    if balance < 100{
      print("Alert: Your balance is under $100")
    }
  }
  
}

I thought didSet was an Apple defined function?

Yes, but the fact that it’s not recognizing it should be a red flag, and a call to documentation (I just googled swift documentation didSet). You’ll notice that properties are not functions/methods, and therefore it doesn’t make sense to have a property observer to something that’s not a property (didSet is a property observer).

So to be clear in the scope of a function, didSet means nothing to the compiler.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties

Properties

Properties associate values with a particular class, structure, or enumeration. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures.

In its simplest form, a stored property is a constant or variable that’s stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).

Property Observers

Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.

You can add property observers in the following places:

Stored properties that you define

Stored properties that you inherit

Computed properties that you inherit
1 Like

Your explanation, was, on point. Thank you very much! (:

1 Like