![]() ![]() The is operator has two related but technically distinct uses in Swift.įirst, if the right-hand term is a class name, it returns a Bool regarding whether the left-hand term is indeed that subclass. This is just for a quick reference, let’s get a bit more in depth on what these actually do. Nonetheless, here is a handy table to show them all in one place and what they return: Operatorīool, if the entity is indeed one of its subclasses or adopts that protocolįorcefully unwrapped optional of intended entity. One could make the argument that they are really 2, since the last two are the same operator with an optional and non-optional form. There are 3 type casting operators in Swift with extremely simple names: is, as?, and as!. While this term is not used in Apple’s iBook, it is used in the WWDC video “Swift Interoperability In Depth.” It of course means to go the other way, casting a derived class back up to one of its base classes. There is an opposite term to this one, the obviously named upcast. According to Wikipedia, downcasting is the act of casting a reference of a base class to one of its derived classes. There is one term that is used a lot when talking about type casting, so it should probably be defined upfront, that term is downcast. Type casting is a way to convert an object of one type to another. It was simple enough to mention there, but I felt I should cover type casting in Swift a bit more in-depth in its own post. In my previous post, Generic Functions in Swift, I used a type casting operator to check for inheritance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |