How to convert [CustomClass] to [AnyObject]

2.1k views Asked by At

I have an array of custom class [CustomClass]. I am trying to convert it to [AnyObject].

self.customClassArray = self.anyObjectArray as [AnyObject]

I get the following errors:

'AnyObject' is not a subtype of 'CustomClass'

and

Cannot convert value of type '[CustomClass]' to type '[AnyObject]' in coercion.

What am I missing?

3

There are 3 answers

2
MarkHim On BEST ANSWER

There is basically no scenario where you would have to cast your CustomClass to AnyObject.

Your custom class is a subtype of AnyObject and not the other way around, therefore any function that requires an AnyObject will be able to handle your CustomClass

1
user3441734 On
class C {
    var i: Int = 0
    func foo() { print(i) }
    init(_ i: Int) { self.i = i }
}

let arrC = [C(1),C(2)]
print(arrC.dynamicType) // Array<C>

let arrO:[AnyObject] = arrC
print(arrO.dynamicType) // Array<AnyObject>

arrO.forEach { (o) -> () in
    //o.foo() // error: value of type 'AnyObject' has no member 'foo'
    (o as? C)?.foo()
}

what you tried to do (if i understood you question) is cast [C] as [AnyObject]

let c = arrC
let o = c as [AnyObject]
print(c.dynamicType, o.dynamicType) // Array<C> Array<AnyObject>

which is valid. If you received an error, please provide more code, so we could see where is your trouble.

The error message seems to be valid if you tried to cast [AnyObject] as [C]. And yes, such a casting is illegal, because AnyObject is not type of C. AnyObject is a protocol to which all classes conform

0
Hitendra Solanki On

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