1.) You can convert superClass object to Subclass Type.
2.) You can't Convert SubClass type of object to SuperClass.
This the general rule of Inheritance.
Same rules applies to protocol adoption also.
E.g. Suppose you have three classes.
Class A,B,C.
Class A {
}
Class B is sub class of A.
Class B : A {
}
Class C is sub class of A.
Class C : A {
}
In other words you can say A is superClass of class B and class c.
you have objects as below.
var objA = A()
var objB = B()
var objC = C()
In this case, you can convert objA to type of Class B or C. i.e.
let objB1 = objA as! B //success
let objC1 = objA as! C //success
But you can't manually convert objB or objC to type of Class A. i.e.
let objA1 : A = objB as! A //will not allowed
You can store object of type Class B or C to type of class A without casting. i.e.
let objA2 : A = objB; // objB is type of class B.
let objA3 : A = objC; // objC is type of class C.
Then later on you can caste objA2 to type of class B when ever reqired, like
let objB3 = objA2 as! B
AnyObject is a protocol in Swift.
The protocol to which all classes implicitly conform.
@objc protocol AnyObject
You do not need to implement this protocol manually, its implicit.
Class A,B,C also adopts the protocol AnyObject,
so you can store object of A, B or C to type of AnyObject.
like our above statement:: let objA2 : A = objB; // objB is type of class B.
let anyObj1 : AnyObject = objA
let anyObj2 : AnyObject = objB
let anyObj3 : AnyObject = objC
you could not manually cast to type AnyObject. you can store directly as above.
Then later on you can convert,
anyObj1 to type A
anyObj2 to type B
anyObj3 to type C