Swift - an array of down arrays will not compile

How can I allocate an array of AnyObject arrays into an array of String arrays ?

I tried the following code:

 let v1:[[AnyObject]] = [["hello"]] // v1 type is [[AnyObject]] let v2 = v1 as! [[String]] // compile error! 

but this code will not compile with an error:

'String' is not identical to "AnyObject"

if I'm just trying to convert an AnyObject array to a String array, it works fine:

 let v1:[AnyObject] = ["hello"] // v1 type is [AnyObject] let v2 = v1 as! [String] // v2 type is [String] as expected 
+5
source share
1 answer

You have already answered your question. Do in the first code for each element of the array what you successfully do in the second code. Like this:

 let v2 = v1.map {$0 as! [String]} 
+9
source

All Articles