Describes the attributes declaration and function in Swift, and describes the attributes declaration in swift.

Source: Internet
Author: User

Describes the attributes declaration and function in Swift, and describes the attributes declaration in swift.

I. Introduction

The attribute associates the value with the class, struct, and enumeration. Swift has two types of attributes: storage attributes and computing attributes. Storage attributes are used to store a value. They can only be used for classes and struct. Computing attributes are used to calculate a value. They can be used for classes, struct and enumeration.

Ii. Storage attributes

Storage attributes use variables or constants to store a value. When declaring a storage attribute, you can set a default value or set a value in the construction example, the properties can be accessed through the dot syntax. The storage attribute sample code of the struct is as follows:

Struct MyStruct {var property1 = 1 var property2: Int} var obj = MyStruct (property1: 1, property2: 2) // access print (obj. property1, obj. property2)

If an attribute is declared as a let constant, the attribute cannot be modified. Note that if you use let to create a struct instance, you cannot modify the attributes of the struct even if they are variables. This is very different from the class.

Another type of storage attribute is the Delayed Storage attribute. You can imagine that some attributes of a class may not be used after each class instance, in addition, the construction of some attributes may consume a lot of time. In this case, a clever design is that such attributes are not constructed during class instantiation, this attribute is constructed only when the class instance uses this attribute. This attribute is called a Delayed Storage attribute and is declared using the lazy keyword. The example is as follows:

// The First class MyClass1 {init () {print ("MyClass1 class constructed ")}} class MyClass2 {// declared as the Delayed Storage property lazy var body = MyClass1 ()} // when constructing MyClass2, no body attribute construction will be performed. No printed information will be generated. var obj2 = MyClass2 () // After executing the following code, a printed information will be generated. The body attribute is used to construct the obj2.body.

Note: If the delayed constructor attribute is used in multiple threads, it cannot be constructed only once.

Iii. Computing attributes

In a simple understanding, a calculated attribute is not an independent attribute used to store values. developers can even regard it as a calculation method, it is mainly used to obtain or set values of other storage attributes through calculation. Example:

Struct Circle {// center var center :( Double, Double) // radius var r: Double // perimeter it as the calculation attribute var l: double {get {// calculate the circumference of the circle return 2.0 * r * M_PI} set {// re-calculate the radius by perimeter the default input parameter is newValue r = newValue/(M_PI * 2 )}}} var circle = Circle (center: (0, 0), r: 2) print (circle. l) circle. l = 24 print (circle. r)

The Demo code above shows that the l attribute is not a new attribute, but is calculated by the r attribute or reverse r through the l attribute. Note one of them, two code blocks, set and get, can be created in the calculation attribute. The set code block is optional. A newValue parameter is generated by default to pass data transmitted from outside. The get code block must be implemented, of course, only get code blocks can be implemented. In this case, this attribute will be a read-only computing attribute, which can only be obtained and cannot be set. Note that the developer can also customize a parameter name after the set code block to receive parameters passed by the outside world. The example is as follows:

Struct Circle {// center var center :( Double, Double) // radius var r: Double // perimeter it as the calculation attribute var l: double {get {// return 2.0 * r * M_PI} set (newL) {// re-calculate the radius by perimeter. The default input parameter is newValue r = newL/(M_PI * 2 )}}}

The read-only computing attribute can be further abbreviated. Because no set code block is available, the keyword get and parentheses can also be omitted without ambiguity. The example is as follows:

struct Point {  var x:Double  var y:Double  var center:(Double,Double){    return (x/2,y/2)  }}

Iv. Property listener

The get and set methods in the calculation attributes of Swift are not the same as the get and set methods in Objective-C, objective-C provides the set and get methods for developers to perform custom operations when the attributes are to be obtained or set, this part of development needs are implemented through the property listener in Swift.

Property listeners include willSet and didSet. willSet is executed when the property value is about to change, and didSet is executed when the property value has changed, and the values before and after the change are passed in. Example:

Struct Point {var x: Double var y: Double {willSet {print ("Update setting of the value to be performed, new value:", newValue )} didSet {print ("the settings are worth updating. The old values are:", oldValue) }}var center :( Double, Double) {return (x/2, y/2) }}var point = Point (x: 3, y: 3) // print/* to update the value. The new value is: 4.0 is worth updating. The old value is 3.0 */point. y = 4

By default, willSet generates a parameter named newValue. In didSet, a parameter named oldValue is generated by default. You can also customize the names of these parameters, as shown in the following example:

Struct Point {var x: Double var y: Double {willSet (new) {print ("set to update the value, the new value is:", new )} didSet (old) {print ("it is worth updating settings. The old value is:", old) }}var center :( Double, Double) {return (x/2, y/2 )}}

V. instance attributes and type attributes

The instance attribute is for an instance of the same type, and the type attribute is for the instance directly. Each time a type is instantiated, its instance has a set of independent instance attributes, and the type attribute is shared by all instances of the class. In Objective-C, generally, the global attribute is used to achieve this effect. In Swift, the static keyword is used to declare the type attribute. The example is as follows:

Struct Point {// type storage property static var name: String = "Point" // type calculation property static var subName: String {return "sub" + name} var x: double var y: Double {willSet (new) {print ("to update the value, the new value is:", new)} didSet (old) {print ("the settings are worth updating. The old values are:", old) }}var center :( Double, Double) {return (x/2, y/2) }}// type attributes get print (Point. name, Point. subName)

Note that there is a special case for computing attributes of the class type. If the subclass needs to be inherited and overwritten, You need to replace the static keyword with the class keyword. The example is as follows:

Class SomeClass {static var storedTypeProperty = "Some value." static var computedTypeProperty: Int {return 27} // class var overrideableComputedTypeProperty: Int {return 107 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.