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:
ifis Apple{ println("这是个苹果") //某个类的对象一定是自己这个类}ifis Fruit{ println("这是个苹果") //某个类的对象也一定是它的超类}ifis Apple{ println("这是个水果") //但超类的对象不是它的子类}ifis Orange{ println("这是个橘子") //没有继承关系的类肯定返回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:
vararray[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:
// 这里假设fakeFruit是一个表现为Fruit类的Apple类对象varas! Apple //这是as!的最常见用法,可以成功转换varas! Fruit //成功,子类可以向上转换为超类varas//失败,没有继承关系的类不可以转换varas//失败,超类不可以强制转换为子类
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:
ifvaras? 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.
See the full column-"Swift easy Getting Started"
"Introduction to Swift (i)--basic syntax"
"Introduction to Swift (ii)--character and string"
"Introduction to Swift (iii)--tuples (tuple)"
"Introduction to Swift (iv)--optional type (optionals) and assert (Assert)"
"Getting Started with Swift (v)--Arrays (array)"
"Introduction to Swift (vi)--Dictionary (Dictionary)"
"Introduction to Swift (vii)-structure (struct)"
"Introduction to Swift (eight)--powerful redundancy operator"
"Introduction to Swift (ix)--string and int, Double, float and other numbers convert each other"
"Introduction to Swift (10)-circular reference, weak reference, and no master reference"
"Introduction to Swift (11)--type conversion with IS, as Operation"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Swift (11)--type conversion with IS, as Operation