Last active
May 10, 2016 03:27
-
-
Save 5SMNOONMS5/628c2b889a61dd1a0a4aec654add948f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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