I am just getting started with swift and I know we have a default array initialiser in swift and the syntax goes like this:
let myArray = [Int](count:3 , repeatedValue:2)//int types [2,2,2]
But when I remove [Int] from the statement,it initialises the array with values (3,2).
let myArray = (count:3 , repeatedValue:2)//[3,2]
Can anyone explain this behaviour?
In the second example, you're getting a tuple, not an Array
. If you don't want to specify [Int]
, you still need to specify Array
, like this:
let myArray = Array(count: 3, repeatedValue: 2)
Learn more about tuples in the Swift book.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.