Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active August 29, 2015 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/1b0c957fe112a61760ea to your computer and use it in GitHub Desktop.
Save krzyzanowskim/1b0c957fe112a61760ea to your computer and use it in GitHub Desktop.
How to use class constant to initialize variable - solution is use lazy variable
import Foundation
class Foo {
let length = 255
// !!! not like this because self.length can't be used here
// var arrayConstantLength = [Byte](count:self.length, repeatedValue:0)
// but like this (with lazy I can use self.length constant)
lazy var arrayConstantLength:[Byte] = {
[unowned self] in return [Byte](count: self.length, repeatedValue: 0)
}()
}
let q = Foo().arrayConstantLength
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment