Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using type methods to initialize lazy stored properties
class FibonacciNumbers {
static 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
}
}
class Maths {
lazy var fibonacciNumbers = FibonacciNumbers.createFirstFewFibonacciNumbers()
/*
Other stuff here! e.g.
let pi = 3.14159265359
*/
}
let instance = SomeClass()
instance.fibonacciNumbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment