-
-
Save Pasanpr/c5351ddedd441e214bf8f496818b11ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fibonacci { | |
var value: Int | |
init(value: Int) { | |
self.value = value | |
} | |
lazy var fibonacci: () -> Int = { [unowned self] in | |
// Some temporary variables. | |
var a = 0 | |
var b = 1 | |
// Add up numbers to the desired iteration. | |
for _ in 0..<self.value { | |
let temp = a | |
a = b | |
b = temp + b | |
} | |
return a | |
} | |
deinit { | |
print("\(value) is being deinitialized. Memory deallocated") | |
} | |
} | |
let f = Fibonacci(value: 7) | |
f.fibonacci() | |
//var t: Fibonacci? = Fibonacci(value: 8) | |
//t?.fibonacci() | |
//t = nil | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment