Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created February 13, 2017 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pasanpr/c5351ddedd441e214bf8f496818b11ea to your computer and use it in GitHub Desktop.
Save Pasanpr/c5351ddedd441e214bf8f496818b11ea to your computer and use it in GitHub Desktop.
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