Skip to content

Instantly share code, notes, and snippets.

@5SMNOONMS5
Last active May 10, 2016 03:27
Show Gist options
  • Save 5SMNOONMS5/628c2b889a61dd1a0a4aec654add948f to your computer and use it in GitHub Desktop.
Save 5SMNOONMS5/628c2b889a61dd1a0a4aec654add948f to your computer and use it in GitHub Desktop.
class Foo {
var name: String? // instance property
/**
You define type properties with the static keyword.
For computed type properties for class types,
you can use the class keyword instead to allow subclasses to override the superclass’s implementation.
The example below shows the syntax for stored and computed type properties
*/
static var all = [Foo]() // static type property
/*
For type property
you must always give stored type properties a default value.
This is because the type itself does not have an initializer that
can assign a value to a stored type property at initialization time.
Stored type properties are lazily initialized on their first access.
They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously,
and they do not need to be marked with the lazy modifier.
*/
class var comp: Int { // computed type property , always need default Value
return 22
}
class func alert() { // type method
print("There are \(all.count) foos")
}
}
Foo.alert() // There are 0 foos
let f = Foo()
Foo.all.append(f)
Foo.alert() // There are 1 foos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment