A class is made up of properties and methods, which are generally accessed by data members, and in Object-c, properties are designed to access the encapsulated data members, the properties themselves do not store data, the data is stored by data members, and the properties in swift are stored and computed properties. The stored property is the data member in the OBJECT-C, the computed property does not store the data, but you can return the value by calculating the other property
Storage properties can store data, divided into constant attributes and variable properties
Storage properties apply to class and struct body object-oriented struct types, enum properties do not store properties
1. Storage attribute Concept
Class employee{Let No:int = 0 var name:string = "" var job:string? var salary:double = 0 var dept:department? }struct Department {Let no:int = 0 var name:string = ""}
var emp = Employee () emp.no = 100//Compile Error-----(1) Let Dept = Department () dept.name = "Sales"//Compile Error---(2) let EMP1 = Emplo Yee () Emp1.name = "Sales"-------(3)
The first line of the code modifies the constant property, the program compiles the error, and the second line of code also has an error, because the instance dept itself is a constant, even if its property name is a variable and cannot be modified, the third line of code can be compiled through, because EMP1 is a class instance, a reference type, and Depet is a struct instance , is a value type. A reference type is equivalent to a pointer, and its variables can be modified, but the change of value type is also non-modifiable
2. Delayed Storage Properties
The program doesn't care which department he belongs to, only cares about its no and name properties, although it does not use Dept instances, but still consumes memory. In Java, there is a technique for data persistence called Hibernate,hibernate, which has a delay-loading technique, and Swift also consumes the delay-loading cardinality, as shown in the following example:
Class employee{Let No:int = 0 var name:string = "" var job:string? var salary:double = 0 lazy var dept:department? = Department ()}struct Department {Let no:int = 0 var name:string = ""}
The Dept property is preceded by the lazy keyword, so that the Dept property is lazy loading, as the name implies, is that the Dept property is only loaded the first time it is accessed, and if never accessed, it will not be created, thus reducing memory consumption
This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1621819
Swift Storage Properties