Swift Learning Note 4

Source: Internet
Author: User

1. The deferred storage attribute is the attribute whose initial value is computed when the first call is taken. Used before the property declaration lazy to indicate a deferred store property.

You must declare the deferred store property as a variable (using the var keyword), because the initial value of the property may not be available until the instance is constructed. The constant property must have an initial value before the construction process is complete, and therefore cannot be declared as a deferred attribute.

If a property that is marked is lazy accessed by multiple threads at the same time without initialization, there is no guarantee that the property will be initialized only once.

2. Calculated properties do not store values directly, but instead provide a getter and an optional setter to indirectly get and set the values of other properties or variables. You can use the default name if the setter for the computed property does not define a parameter name that represents the new value newValue .

 var center:point { get   {let CenterX  = origin.x + (size.width/2  ) let CenterY  = origin.y + (size.height/2  )  return   point (X:centerx, Y:center Y)}  set   {origin.x = newvalue.x-(size.width/2   = newvalue.y-(size.height/2   

You must use var keywords to define calculated properties, including read-only computed properties, because their values are not fixed. letThe keyword is used only to declare a constant property, which is a value that cannot be modified after initialization.

The declaration of a read-only computed property can be stripped of get keywords and braces:

var volume:double {        return width * Height * depth    }

3. property observers Monitor and respond to changes in property values, and each time a property is set to a value, the property observer is called, even when the new value is the same as the current value.

You can add property observers for storage properties other than deferred storage properties, or you can override properties to add property observers for inherited properties, including storage properties and computed properties.

You do not need to add a property observer for a non-overridden computed property because it can directly monitor and respond to changes in values through its setter.

You can add one or all of the following observers for a property:

    • willSetCalled before the new value is set
    • didSetCalled immediately after a new value has been set

When a property of a parent class is assigned a value in the constructor of a subclass, its and observers in the parent class are willSet didSet called.

0 {        Willset (newtotalsteps) {            print ("about toset Totalsteps to \ (newtotalsteps)  ")        }        didset {            if totalsteps > oldValue  {                print (  "Added \ (totalsteps-oldvalue) steps")     }}}

4. Global constants or variables are deferred calculations, similar to deferred storage properties, except that global constants or variables do not require tag lazy modifiers.
Local scope constants or variables are never deferred.

5. The stored type attribute can be a variable or constant, and the computed Type property is defined as a variable attribute only as the computed property of the instance.

Unlike the stored-type properties of an instance, you must specify a default value for the stored Type property, because the type itself does not have a constructor, and you cannot use the constructor to assign a value to the Type property during initialization.
Storage type properties are deferred initialization and are initialized only the first time they are accessed. Even if they are accessed by multiple threads at the same time, the system is guaranteed to initialize it only once and does not need to use modifiers on it lazy .

Use keywords static to define type properties. When you define a computed type property for a class, you can use the keyword instead to class support subclasses overriding the implementation of the parent class.

Swift Learning Note 4

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.