Type Casting
Type casting in Swift is implemented with is and as operators. Type casting: A subclass instance can be use as a superclass instance. Defining a Class Hierarchy class Media { var name: String init(name: String) { self.name = name } } class Song: Media { var artist: String init(name: String, artist: String) { self.artist = artist super.init(name: name) } } class Movie: Media { var director: String init(name: String, director: String) { self.director = director super.init(name: name) } } var library = [ Movie(name: "A", director: "Michael"), Song(name: "B", artist: "Elvis"), Movie(name: "C", director: "Orson"), Song(name: "D", artist: "Rick"), Media(name: "E") ] // The "library" is inferred to be [Media] for item in library { print(item.name) // Here the item is of "Media" type } // If we want to use the library's item as the subclass instance, we should downcast the item. Checking Type Use is to check whether an instance is of a certain subclass type. ...