Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using a method to initialize a lazy stored property
class SomeClass {
lazy var fibonacciNumbers = self.createFirstFewFibonacciNumbers()
func addOneMoreFibonacciNumber() {
let currentNum = fibonacciNumbers.last!
let prevNum = fibonacciNumbers[fibonacciNumbers.count - 2]
let newNum = currentNum + prevNum
fibonacciNumbers.append(newNum)
}
private func createFirstFewFibonacciNumbers() -> [Int] {
var fibonacciNumbers = [0, 1, 1]
for index in 2...8 {
let currentNum = fibonacciNumbers[index]
let prevNum = fibonacciNumbers[index - 1]
let newNum = currentNum + prevNum
fibonacciNumbers.append(newNum)
}
return fibonacciNumbers
}
}
let instance = SomeClass()
instance.addOneMoreFibonacciNumber()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment