cpkeron.blogg.se

Swift downcast
Swift downcast













swift downcast

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.

swift downcast

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. Take a look at this example.In my previous post, Generic Functions in Swift, I used a type casting operator to check for inheritance. A class instance of type AnyObject isn't always what you need or want. But we need to pay a small price for that flexibility. The AnyObject protocol seems to add a bit of flexibility to the Swift language. For this reason, imported Objective-C types frequently use AnyObject as the type for properties, method parameters, and return values. The flexible behavior of the AnyObject protocol is similar to Objective-C's id type. The Swift standard library confirms this. The AnyObject protocol is similar and it helps bridge the gap between Swift and Objective-C. The id type is used to point to an Objective-C object. If you're familiar with Objective-C, then you've most likely come across the id keyword. AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol. You use AnyObject when you need the flexibility of an untyped object or when you use bridged Objective-C methods and properties that return an untyped result. And that's also what the comments of the AnyObject protocol tell us. var myObject: AnyObjectīecause the myObject variable is of type AnyObject, we know that the value stored in myObject is an instance of a class. But what does it mean? And why is that useful or important? Let's revisit the example we added to the playground earlier. Every class implicitly conforms to the AnyObject protocol. This summarizes what the AnyObject protocol is and what you need to know about it. The protocol to which all classes implicitly conform. The comments are very telling and answer some of the questions you might have. More interesting are the comments above the definition. AnyObject is defined as a public type alias. Press Command and click AnyObject to navigate to the interface of the AnyObject protocol. Let's dive into the bowels of the Swift standard library to learn more about the AnyObject protocol. Xcode also tells us that AnyObject is a protocol to which all classes implicitly conform. Take a look at Xcode's autocompletion suggestions when you type AnyObject.ĭid you notice the letter T on the left? Xcode's autocompletion menu shows us that AnyObject is a type. If you pay close attention, you can see a first clue. Name the playground What The Swift, tell Xcode where you'd like to save the playground, and click Create.Ĭlear the contents of the playground and declare a variable, myObject, of type AnyObject. Choose the Blank template from the iOS section. Get Your Hands Dirtyįire up Xcode and create a new playground.

swift downcast

Let's take a look and dive into the bowels of the Swift standard library to find out. If you've spent some time writing Swift, then you've probably come across a variable, constant, or parameter of type AnyObject.

#Swift downcast series#

If you are curious about the more subtle details of the Swift language, then this series is for you. I document my findings in a series I named What The Swift. Every now and then, I take some time out of my day to explore something about the Swift language that I don't know yet.















Swift downcast