Skip to content

Instantly share code, notes, and snippets.

@gfontenot

gfontenot/properties.swift

Last active Aug 29, 2015
Embed
What would you like to do?
// 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