Swift's properties, methods, subscript scripts, and inheritance

Source: Internet
Author: User


From this chapter, the most important content of the Swift programming language Guide is the concept, not too much code. Understanding Swift's object-oriented philosophy, syntax, and class structure, the construction process is a great help for very good application of the swift language.


Property
Store property Store properties are generally those that can be obtained by directly assigning a value, or by directly visiting a member. It has something to note: If a struct is declared as a constant, the child property cannot be changed. In Objective-c, we can never change the sub-properties of a struct, but Swift does, but it is an exception, and when your stored-type attribute is a struct and a constant, you should stop trying to change the structure's sub-attributes. Classes are reference types, and we can change them even if they are declared as constants.
Delay loading Storage Properties Delay loading We also call lazy loading, which provides a @lazy for declaring lazy loading in swift. @lazy decorated property must be a variable, due to the pass-through property we have to assign a value before the construction is complete, based on this feature, the constant cannot be lazy loading.


This is where we call the function of the DataList, and when we visit it, we have to make a decision about whether or not to load it.

Computed attribute computed properties typically do not store values directly, and getter to get the value, setter to indirectly set the value of other properties or variables.

In a struct rect, Midpoint center is calculated by calculating its own origin and dimension data. The area variable simply declares the Get method, which we default to is a read-only computed property. In fact, we can omit get{} just by reading the computed properties.

var Area : Double {returnsize. width * size . Height }



Property Monitor
In Objective-c, we have the KVO mechanism to flexibly monitor property changes, which are implemented in swift using the property monitor. willset{} system callback before setting a new value, if not named, then the new value with NewValue represents didset{} after the system callback, if not named, provide the previous value oldvalue.


A simple pedometer class that outputs the console before and after setting the value. Note that there is a single case, but it seems that Apple does not yet have a recommendation on Swift's single-instance practice.
Note that the shared variable is decorated with Classkeyword, while the variable in the struct inner uses the static modifier. Both represent type attributes in a class or value type, and all instances share this property.

Method
Similar to Ojbective-c, Swift also has a class method and an instance method, which is also very easy to use, and the instance directly declares that Func is possible, and the class needs to be modified with classes. But sometimes we prefer to call it a function.

Mutation (mutating) under normal circumstances, a value type's attribute cannot be changed in its instance method, and the reference is the same, assuming that the change needs to be decorated with Mutatingkeyword before the method.

In this case, we need to extend the point structure that we just defined, and modify its properties in the extension, so we need to mutating the function.
In the mutation method we can assign the same value to self.

A simple enumeration that completes the switch to self in the next () function.
Class method
As mentioned above, class method value types such as structs use static, whereas reference types, such as classes, use class.


Using the class method here is a simple factory method, in fact in Swift, if you simply assign values to a property, we can use the handy (self-defined) constructor, which is later written to the constructor. In addition, if we do not have an instance, for example in a class method, self is used, then self points to the type and not to the instance. For example, [[Self alloc] init] in objective-c.

Subscript Script (subscripts)
This concept is not in objective-c, we say that swift syntax is more flexible and powerful, because it extends and supports the powerful functions of structs, enumerations, functions, such as functions that belong to the first-class level in Swift.
Here, subscript scripts are often used to define the class, struct, and enumeration in which we can define our own quick access by using subscript scripting. For the same target we are able to access the subscript script in multiple ways by defining how its function overloads.
Basic syntax and use:




Defines a three times-fold calculation structure constant, and then passes in the index and gets the value that the script calculates.
Just note that the subscript script does not agree to set the default value, and does not agree to use InOut to change the number of parameters. A simple example:


Here we use the subscript to get a high-speed access to the values in the corresponding position of the data list held by this class instance, without having to write an access to the array they hold every time you visit the corresponding obj.

Inherited
In Swift, inheritance is a fundamental feature that distinguishes between "classes" and other types.
Base class
Classes that differ from Nsobject,swift in OC are not inherited from a common base class. But very versatile Swift is achieved by providing a standard library.
Sub-class generation
Identify the inheritance relationship directly through ":". Subclasses are required to initialize the inherited properties when they attempt to change them.

Rewrite
Using Overridekeyword
When overriding a property, the name and type must be fully written, enabling the compiler to check whether your overridden property matches the superclass. You can rewrite a read-only property to be read-write, but you cannot rewrite read-write (ReadWrite) as just reading (readonly).
In addition, we can override the property observer, but we cannot override the observer for properties that are read-only and those that already have setters.
As with Java, if the override is prevented, it is declared with @final as the last version number.


The above is all the content of this blog, Welcome to contact me for discussion and errata.

Swift's properties, methods, subscript scripts, and inheritance

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.