Three kinds of operation: is, as? and as!
Swift is a strongly typed language, but also allows developers to pass the IS, as? and as! These three operations are to determine and cast the type. Where are is used as type judgments, and as? and as! is the optional and mandatory form of the type conversion, respectively. It is emphasized here that the more commonly used downward conversions (downcast) in Swift are not as operators.
To make it easier to explain later, there are three classes defined, Fruit, Apple, and Orange, where both apple and orange inherit from Fruit.
Is operator
The is action is used to determine whether an object is a particular class, and it returns a value of type bool. The logic of the is operation is simple, the object of one class must be its own class, and it must be its own superclass, but the object of the superclass is not a subclass. If two classes do not have an inheritance relationship, the is operation must return FALSE.
Let's take a look at the following code for a few examples:
IfAppleIs Apple{println("It's an apple.") The object of a class must be itself this class}IfAppleIs Fruit{println("It's an apple.") The object of a class must also be its superclass}IfFruitis apple{ Println ( "This is a fruit" ) //but superclass object is not its subclass }< Span class= "Hljs-keyword" >if Apple is orange{ println< Span class= "pun" > ( "This is an orange" ) //a class that does not have an inheritance relationship must return False}
It is important to note that the class of an object referred to here refers to its real class rather than the displayed class. For example, put Apple1 and orange1 into the array, and then take out, because the type of the array is [Fruit], so the following line of code:
var fruit = array[0]
The fruit is taken out, although it is fruit in swift, but it is actually an object of the Apple class, which is considered as an object of the Apple class when it is used to manipulate it. Therefore, although the object of the superclass is not a subclass, the return value of fruit is Apple should be true.
as! Operator
as! An operator is a forced form of a type conversion, with the advantage that the code is simple and if it can be converted, the converted object is returned, and a run-time error is thrown if the conversion is not possible. Therefore, you should not use as! unless you are absolutely sure that you can convert To perform a forced type conversion.
Very similar to the IS operator, the rule of type conversion is that an object of a class can be converted to its own class (this is nonsense), the subclass can be converted up to a superclass, but the superclass cannot be converted down (downcast) to subclasses. Unless the object representation of a subclass is a superclass, but is actually a subclass, you can use the as! Perform a Down conversion (downcast).
Use the code to illustrate:
This assumes that Fakefruit is an Apple class object that behaves as a fruit classVarFruit1=FakefruitAs! Apple This is as!. The most common usage of the, can be converted successfullyVarFruit2=FakefruitAs! fruit //succeeded, subclass can be converted up to superclass var fruit3 =< Span class= "PLN" > Fakefruit as! Span class= "Typ" >orange //failed, class with no inheritance relationship cannot be converted var Orange = Realfruit as! orange //failed, superclass cannot be cast to subclass
As Operator
As and as! The conversion rules for the operators are exactly the same, but as? will return an optional type of the converted type, which requires us to unpack. So the wording would be slightly different, in the first example:
if var fruit = fakeFruit as? Apple{ //Do something with fruit}
In addition, because it is an optional type, it is recommended to use this method for type conversion, even if the conversion fails without an error.
Introduction to Swift (11)--type conversion with IS, as Operation