I am new to Swift, so some of the things I donβt quite understand yet. I hope someone explains this to me:
// Creating Type Properties and Type Methods class BankAccount { // stored properties let accountNumber: Int let routingCode = 12345678 var balance: Double class var interestRate: Float { return 2.0 } init(num: Int, initialBalance: Double) { accountNumber = num balance = initialBalance } func deposit(amount: Double) { balance += amount } func withdraw(amount: Double) -> Bool { if balance > amount { balance -= amount return true } else { println("Insufficient funds") return false } } class func example() { // Type methods CANNOT access instance data println("Interest rate is \(interestRate)") } } var firstAccount = BankAccount(num: 11221122, initialBalance: 1000.0) var secondAccount = BankAccount(num: 22113322, initialBalance: 4543.54) BankAccount.interestRate firstAccount.deposit(520)
So this is the code. I am wondering why deposit() does not have a return arrow and returns a keyword and withdraw() . When I use the return arrow, in what situations is there a rule or something else? I do not understand.
Besides ... Everyone is so kind to your answers, now it becomes clear to me.
At the beginning of this tutorial there is practical code for functions
// Function that return values func myFunction() -> String { return "Hello" }
I assume that this return value is not required here, but in the textbook they wanted to show us that it exists, am I right?
Also, can I make a βmistakeβ and use the back arrow and value in my deposit function? I tried with this:
func deposit(amount : Double) -> Double { return balance += amount }
... but he generated an error.
I saw advanced coding in my last company, they were creating an online store with a lot of customizable and interesting features, and the whole code was full of return arrows. This confused me, and I thought it was the default for creating methods / functions in OOP.
Additional question! I wanted to play with functions, so I want to create a transferFunds () function that transfers money from one account to another. I made such a function
func transferFunds(firstAcc : Int, secondAcc : Int, funds : Double) { // magic part if firstAcc == firstAccount.accountNumber { firstAccount.balance -= funds } else { println("Invalid account number! Try again.") } if secondAcc == secondAccount.accountNumber { secondAccount.balance += funds } else { println("Invalid account number! Try again.") } }
This is a simple code that came to my mind, but I know that it can even be stupid. I know that there should be a code that checks if there are enough funds in the first account, from which I take money, but everything is in order ... I'll play with this. I want to specify accountNumbers or something else in the parameters in the transferFunds() function, and I want to search for all objects / clients in my imaginary bank that use the BankAccount class to find it and then transfer the money. I do not know if I described my problem correctly, but I hope you understand what I want to do. Can someone help me please?