Apple gives us an example of downcasting an object of the same type as such:
let someObjects: [AnyObject] = [
Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"),
Movie(name: "Moon", director: "Duncan Jones"),
Movie(name: "Alien", director: "Ridley Scott")
]
We can then access each individual attribute by creating an abstract variable "object" and casting it as what we expect it to be (a movie) :
for object in someObjects {
let movie = object as! Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
But what if we go to the next level and for example we have subclasses of movies:
Silent films
Comedy
Action
Each of which all have the same attributes - name & director
I've tried to cast it the same way we did above as a "Movie" since I assumed as a parent class, it would be able to recognize its subclasses, but of course this has not worked otherwise this question would not have existed!