Swift's optional chain, type conversion and expansion

Source: Internet
Author: User


Optional chain (Optional Chaining)
An optional chain is a request or invocation property. method, the process of the sub-script. The optional expression is that the target of the request or call may currently be nil. If it is not nil, it is called successfully. Otherwise, nil is returned and the chain is invalidated.


The return result of the invocation of an optional chain is the same as the original result type, but is packaged as an optional type optional.


This is because Roomcount returns nil so the run Else statement assigns a value of-1 to count.
Note: When the BETA3 version number was announced, Swift was changed slightly, Nil became keyword, and no longer had the Niltype nil.
The optional chain is capable of continuous multi-layer invocation, if the example: John.residence?. Address?

. Street?. Description ()

What if there is a way to return optional?

Behind the parentheses.



Type conversions
Type conversions are a way to check the instance type, as well as to make the instance a parent or subclass of a class.

Use is or as in swift to judge and judge whether they are a class or a protocol.
IS is used to check whether an instance belongs to a particular subtype, or False if it is true. Note that this is a specific subtype. Suppose you define a class and instantiate it. Then check if it is this class, and the compiler will give you an error to tell you that this is always returns TRUE.




Down transformation
Because the downward transformation may fail, the type conversion operators have two different forms.
As? Returns a optional value. That is, nil is not successfully returned. As is a combination of downward transformation and forced unpacking, and unsuccessful execution error.


The downward transition is the same as the strong oc,java in the. There is no real change in the example. Simply tell the compiler to use it as a class.



Any and Anyobject
There are no all class base classes like NSObject in Swift.

But there are anyobject that can represent instances of whatever class type. Any can represent no matter what type, except the method type.

When you need to use Cocoaapis, you generally receive an array of [Anyobject]. An array of types is not understood in OC. The pointers stored in them can point to instances of whatever class.


Use the any type to work with different types of blending, including non-class.



Nested types
In swift, nested types are supported. is as flexible as supporting nested functions.

You can define types that support nesting in enumerations, classes, and structs.



Extension (Extension)
The extension is similar to the category in Objective-c, except that the extension in Swift does not have a name.
Here are a list of things that the extension in Swift can do:
--Adding computed attributes and calculating static properties--defining instance methods or type methods--providing new constructors--defining subscript scripts--defining and using new nested types--making an existing type conform to a protocol

Grammar
Use Keywordextensionextension SomeType {...}

Adaptation protocol: extension Sometype:someprotocol, Anotherprotocol {...} Also known as the inclusion of protocol followers in the extension.
Add a computed type attribute


Note: Extensions can add new computed properties, but cannot add stored properties or add attribute detectors to existing attributes.

Of course we can add a "property" by setting the associated reference. It is necessary to use the knowledge of Swift to invoke Cpointer and the associated reference of Objc/runtime, which is not really a property of significance. And it's just an association.



Constructors
The extension can add a new convenience constructor to the class. However, you cannot add a new specified constructor or destructor. If you use extensions to provide a new constructor, it is the responsibility to ensure that all instances are fully initialized during the construction process.


Method


Use the extension to add a method to int, notice here the: < instead of the original. This is the change that Apple made to the swift syntax when beta3, mainly to distinguish left-right or right-hand.
can also change instances in the extension


Subscript Script


You can even add nested types in the extension.


Demo sample code please click here


The above is all the content of this blog, welcome errata and discussion.

Swift's optional chain, type conversion and expansion

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.