Understanding of computed attributes and previous additions to Swift

Source: Internet
Author: User

The focus of this function should be on the calculation.

For the general properties, either directly to save one, or directly read one, 计算属性 you can be set according to the content, make some modifications or calculations, such as:

Import Uikitclass Sample {        var no1 = 0.0, NO2 = 0.0    var length = 300.0, breadth = 150.0        var middle: (Double,dou BLE) {                get {            return (LENGTH/2, BREADTH/2)        }                //If no parentheses are added, the default is set (NewValue) {}        set {            //can be in SE The T method dynamically modifies other properties and this property            no1 = newvalue.0-(LENGTH/2)            NO2 = newvalue.1-(BREADTH/2)        }               //supplemental        // The Set method can also be written as        //Set (value) {        //     no1 = value.0-(LENGTH/2)        //     NO2 = value.1-(BREADTH/2)        // }    }}var result = sample () print (result.middle) Result.middle = (0.0, 10.0) print (result.no1) print (RESULT.NO2) var result = sample () print (result.middle) Result.middle = (0.0, 10.0) print (result.no1) print (RESULT.NO2)

  

Output Result:

(150.0, 75.0)

-150.0

-65.0

Understanding of computed attributes and previous additions to Swift

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.