Sean Danzeiser Sean Danzeiser - 4 months ago 38
Swift Question

Swift - Cannot convert value of type [MyClass] to expected argument type [AnyObject]

I'm trying to use an objc api from swift. The api looks like this:

public init!(items: [AnyObject]!, reuseIdentifier: String!, configuration: ((AnyObject!, AnyObject!, NSIndexPath!) -> Void)!)


Now, when i try to pass an object into that
items
parameter, like this:

self.service.getObjects(atLatitude: 37.802170, longitude: -122.414197, success: { (objects: [MyClass]) in
self.datasource = DDCollectionViewBasicDatasource(items: stores, reuseIdentifier: "cell", configuration: { (cell, store, indexPath) in

})
}) { (error) in

}


I get the following compiler error:


Cannot convert value of type [MyClass] to expected argument type
[AnyObject]


How can I pass my array into this function??

Thanks

Answer

You can't yet downcast arrays directly in Swift. You have to use map to do it. See answer here:

Swift - downcast array of arrays won't compile