Li Hongqiang iOS Development Swift article -09_ properties

Source: Internet
Author: User

Li Hongqiang iOS Development Swift article -09_ properties

First, definition of Class

The difference between swift and objective-c definition classes

OBJECTIVE-C: typically requires 2 files, 1. h Declaration files and 1. m implementation files

Swift: only 1. Swift files required

Definition format for classes in Swift

1 class name  {2     //... Properties and Method 3}

Second, the attribute

1. What is a property

Properties in Swift, similar to member variables in other object-oriented languages

2. Classification of attributes

According to the official documentation, attributes can be divided into the following types of

(1) Storage attributes (Stored properties)

(2) Calculated attributes (Computed properties)

(3) Type attribute (type properties)

3. Storage Properties

1) Brief description

Store Properties: A stored property is a variable or constant stored in an object (instance)

Storing properties similar to member variables in other object-oriented languages

1 class Person {2     var age:int = $     var height:double = 0.04 let life     = 15}

Description

3 storage properties defined in person class

2 Variables Store Properties: Age of type int, height of double type

1 Constant Storage properties: int type of life

The system does not automatically initialize the above 3 storage properties and requires manual initialization

2) Read and write storage properties

How do I read and write storage properties?

You can read and write the stored properties of an object directly through the dot operator (.)

1 class Person {2     var age:int = "     live =}5" var p = person () 6 p.age = 207 println ("P's life is \ (P.life), P's age is \ (p.age) ")

Description

Line 5th: Create Person Object

Line 6th: Assign a value to the age property of the object P

Line 7th: Accessing the Life attribute value and the Age property value of the object P

3) Deferred storage properties

What is the deferred storage attribute?

Deferred storage properties are properties that are initialized the first time they are used

Using @lazy to identify a deferred storage property

1 class Person {2     @lazy var dog:dog = Dog () 3}4 var p = person () 5 println (P.dog)

Description

The Dog property is not initialized until the 5th line of code is executed to actually create the dog object

Use of deferred storage properties Note: The deferred store property must be a variable and cannot be a constant

The benefit of delaying storage properties: to allow some resources to be loaded again, to avoid unnecessary waste of resources

4. Calculating properties

(1) What is a computed property

Unlike storage properties, computed properties do not store values directly, but rather provide get and set

Get: The process used to take a value and encapsulate a value

Set: The process used to set values and encapsulate values

(2) Code example:

1 class Square {2     //square width 3       var width:double = 0.0 4     //square perimeter 5       var girth:double {6         get {7
   
    //perimeter = width * 4 8                 return width * 4 9         }10         Set (Newgirth) {One             //width = circumference/412                    width = newgirth/413< C13/>}14     }15}
   

Examples of computed properties:

Description

The 3rd line of code: Call the Girth property of get, the output is 40

Line 4th: Call the set of the girth property and pass 200 to the Newgirth parameter

The 5th line of code: the output is 50

(3) Simple set

Set can also not intentionally specify the parameter name of the new value, the default parameter name of the new value is called NewValue

1 var girth:double {2     get {3         return width *     }5     set {6         width = newvalue/47     

Description

When executing the 9th line of code: The value of NewValue in line 6th is 200

(4) Use of calculated attributes note

1) Because the value of the computed property is not fixed, you can only use the Var modifier to evaluate the property, not the Let

1 class Square {2     var girth:double {3         get {4             return girth 5         } 6         Set (Newgirth) {7             girth = NEWG Irth 8         } 9     }10}

Description: The above code throws a dead loop, the 4th line of code raises a loop call get, and the 7th line of code throws a loop call set.

2) A property cannot be both a storage property and a computed property

1 class Square {2     var girth:double = 20.0 {3         get {4             return 10.0 5         } 6         set () {7              8         } 9     }10}

Description: The above code is wrong

(5) Read-only computed properties

What is a read-only computed property? Provides only get, no set computed properties

1 class Square {2     var width:double = 0.0 3     var girth:double {4         get {5             return width * 4 6         } 7     

Description: The 10th line of code will error

Shorthand for read-only computed properties. Read-only computed properties can omit the Get keyword

1 class Square {2     var width:double = 0.03     var girth:double {4         return width * 45     

Description: The 8th line of code will error

5. Type properties

(1) What is a type attribute?

Properties that are decorated with the class keyword are type attributes, or class properties

Class-Modified type properties can only be computed properties, not storage properties

code example:

1 class Circle {2     class var pi:double  {3         return 3.144     }5}

Description: The property pi, defined by line 2nd, is a Type property

(2) Characteristics of type attributes

A class will only have one copy, and multiple instance objects of the class share the only copy

Use of Type property: The Type property does not depend on an object and therefore is accessed using the class name

println (CIRCLE.PI)

Third, Property monitor

1. What is a property monitor?

Sometimes, you need to respond when a property value is modified, in which case you can use the property monitor

Property monitor, you can monitor the modification process of property values

The computed attribute can listen directly to the change of the property value in the set, and the stored property has no set

You can add Willset and didset two property monitors for storage properties

(1) Willset:

Called before a new property value is set

The new attribute value is passed in as a parameter, and the parameter name is newvalue by default

(2) Didset:

Called after a new property value has been set

The old property values are passed in as arguments, and the parameters are oldvalue by default.

2. Code examples

1 class Square {2     var width:double = 0.0 {3         willset {4             println ("Willset---\ (newvalue)") 5         } 6         DIDs ET {7             println ("Didset---\ (oldValue)") 8         } 9     }10}11 var s = Square () S.width = 10

Printing results:

Willset---10.0

Didset---0.0

3. Use note

1 class Square {2     var width:double = 0.0 {3         willset {}4         didset {width =}5}6}7     var s = Square ( ) 8 S.width = 109 println (s.width)

Code Description:

Willset and Didset are not called during property initialization and are called only when the value of the property is set outside the initialization

Initialization of the 2nd line of code does not cause calls to Willset and Didset

Assignment of the 8th line of code will cause calls to Willset and Didset

If you assign a value to a property in the Didset monitor, this value replaces the previously set value

Line 4th assigns a value to the Width property again, overwriting the value assigned to line 8th, so the output of line 9th is 20.

Willset, Didset, and set, get cannot coexist

Li Hongqiang iOS Development Swift article -09_ properties

Related Article

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.