Optional chain (optional chaining)
The optional chain is a process of requesting or calling attributes, methods, and subscripts. Optional because the current request or call target may be nil. If it is not nil, the call is successful. Otherwise, Nil is returned and the chain is invalid.
The returned result of the call of the optional chain is of the same type as the original result, but is encapsulated as an optional type optional.
Here, because roomcount is returned as nil, the execution of the else statement is assigned a value of-1 for count.
Note: Swift was slightly modified when beta3 was released, and Nil was changed to a keyword instead of niltype nil.
The optional chain can be called in multiple consecutive layers. For example: John. Residence ?. Address ?. Street ?. Description () if there is a way to return optional, then? Followed by brackets.
Type conversion
Type conversion is a way to check the instance type and make the instance a parent class or subclass. In swift, use is or as to determine and infer whether they are a class or a protocol.
Is is used to check whether an instance belongs to a specific subtype. If yes, true is returned. If not, false is returned. note that this is a specific subtype. If you define a class and instantiate it, and then check whether it is the class, the compiler will report an error and tell you that this is always true.
Downward Transformation
Because downward transformation may fail, the type conversion operator has two different forms.
As? Returns an optional value, indicating that nil is not successfully returned. As combines downward transformation with forced unpacking. If the conversion fails, the runtime is incorrect.
The downward conversion is the same as the strong conversion in OC and Java. It does not really change the instance, but simply tells the compiler to use it as this class.
Any And anyobject
Swift does not have any base classes like nsobject. However, anyobject can represent any class type instance. Any can represent any type, except the method type. When cocoaapis is required, it generally receives an array of [anyobject], because there is no clear type array in OC, And the stored pointer can point to any class instance.
Use the any type to work with different types of mixed, including non-class.
Nested type
Swift supports nested types, just as flexible as nested functions. You can define nested types in enumeration, classes, and struct.
Extension)
Extension is similar to category in objective-C, but the extension in SWIFT has no name.
The following lists what extensions in Swift can do:
-- Add computing and computing static properties -- Define an instance method or type Method -- provide a new constructor -- Define a subscript script -- define and use a new nested type -- make an existing type conform A protocol
Syntax
Use the keyword extensionextension sometype {...}
Protocol Adaptation: Extension sometype: someprotocol, anotherprotocol {...} is also known as adding protocol compliance to the extension.
Add computing attributes
Note: extensions can add new computing attributes, but cannot add storage attributes or add attribute observers to existing attributes. Of course, we can add an "attribute" by setting the association reference. We need to use the knowledge of swift to call cpointer and the Association reference of objc/runtime. Of course, this is not a true attribute, it is just an association.
Constructor
Extension can add new constructor to the class, but cannot add new specified constructor or destructor. If an extension is used to provide a new constructor, it is the responsibility to ensure that all instances are fully initialized during the constructor.
Method
Add a method for int using extension. Note the following .. <replaced the original .. this is Apple's modification to the swift syntax during beta3, mainly to distinguish between Left-closed, right-closed, and right-open.
You can also modify an instance in the extension.
Subscript script
You can even add nested types in the extension.
Click here for the sample code
The above is all the content of this blog. You are welcome to refer to the error and discussion.
Swift optional chain, type conversion and Expansion