How to declare and initialize array of int in Swift

2 Answers

0 votes
var arr:[Int] = [6, 8, 1, 23, 973, "a"]

print(arr)




/*
run:

swift:1:36: error: cannot convert value of type 'String' to expected element type 'Int'
var arr:[Int] = [6, 8, 1, 23, 973, "a"]

*/

 



answered Feb 7, 2021 by avibootz
0 votes
var arr:[Int] = [6, 8, 1, 23, 973]

print(arr)

for item in arr {
   print(item)
}




/*
run:

[6, 8, 1, 23, 973]
6
8
1
23
973

*/

 



answered Feb 7, 2021 by avibootz

Related questions

1 answer 104 views
2 answers 178 views
1 answer 114 views
1 answer 164 views
164 views asked Sep 1, 2020 by avibootz
4 answers 138 views
...