Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfontenot/19924dace096e89ceed1 to your computer and use it in GitHub Desktop.
Save gfontenot/19924dace096e89ceed1 to your computer and use it in GitHub Desktop.
// stored property, no default value
let foo: String
// stored property, default value
let foo: String = "foo"
// or
let foo = "foo"
// computed property, no custom setter
var foo: String {
// access to other instance properties in here
return "foo"
}
// computer property, custom setter and getter
var foo: String {
get { return "foo" }
set { /* do some stuff with `newValue` */ }
}
// stored property, default value provided by an immediately executed closure
let foo: String = {
// no access to instance properties in here
return "foo"
}()
struct Foo {
static let foo: String = Foo.baz() // this works
let bar: String = quz() // this doesn't, expects to be passed an instance
static func baz() -> String {
return ""
}
func quz() -> String {
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment