| Start |
|---|
| Creating a Variable Declare a new variable with Variables can have their values changed: Unless they're defined with Swift infers the type of variable, so you don't always have to declare variable type: Variable names aren't restricted to letters and numbers - they can also contain most other unicode characters, although there are some restrictions Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number Source developer.apple.com (opens new window) |
| Property Observers Property observers respond to changes to a property's value.
Note:
The parameter names for Caution: While it is supported to declare setter parameter names, one should be cautious not to mix names up:
|
| Lazy Stored Properties Lazy stored properties have values that are not calculated until first accessed. This is useful for memory saving when the variable's calculation is computationally expensive. You declare a lazy property with Often it is assigned to a return value of a closure: Lazy stored properties must be declared with |
| Property Basics Properties can be added to a class (opens new window) or struct (opens new window) (technically enums (opens new window) too, see "Computed Properties" example). These add values that associate with instances of classes/structs: In the above case, instances of These types of properties are considered stored properties, as they store something on an object and affect its memory. |
| Computed Properties Different from stored properties, computed properties are built with a getter and a setter, performing necessary code when accessed and set. Computed properties must define a type: A read-only computed property is still declared with a Read-only computed properties can be shortened to exclude |
| Local and Global Variables Local variables are defined within a function, method, or closure: Global variables are defined outside of a function, method, or closure, and are not defined within a type (think outside of all brackets). They can be used anywhere: Global variables are defined lazily (see "Lazy Properties" example). |
| Type Properties Type properties are properties on the type itself, not on the instance. They can be both stored or computed properties. You declare a type property with In a class, you can use the This is used often with the singleton pattern (opens new window). RemarksProperties: Associated with a type Variables: Not associated with a type See the Swift Programming Language iBook (opens new window) for more information. |
Read Article
https://develop.horje.com/learn/1434/reference