1

I am trying to read nested array as follows but getting an error.

var inputArray = [1,[4,3],6,[5,[1,0]]] 

func nestedArray(inputArray :[Any])
{

}

error: heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional var inputArray = [1,[4,3],6,[5,[1,0]]]

rmaddy
  • 307,833
  • 40
  • 508
  • 550
casillas
  • 15,701
  • 19
  • 102
  • 197

1 Answers1

2

You need

var inputArray:[Any] = [1,[4,3],6,[5,[1,0]]] 

as you specify elements of different types Int , Array and nested Array

Sh_Khan
  • 93,445
  • 7
  • 57
  • 76
  • How could I able to read an item in this nested Array, like first and second item iteratively? – casillas Oct 18 '18 at 21:32
  • you need if else or switch cases of castable options like **element as? Int** , or **element as? [Int]** or **element as? [Any]** respectively – Sh_Khan Oct 18 '18 at 21:34
  • could you please illustrate with an example, I am trying to flatten this nested Array into a regular array? – casillas Oct 18 '18 at 21:35
  • look here https://stackoverflow.com/questions/24465281/flatten-a-array-of-arrays-in-swift – Sh_Khan Oct 18 '18 at 21:37