Extra Bits of Arrays
You can create arrays that are empty or with prepopulated contents. You can create and prepopulate an array by using the extra parameters extra:
and repeatedValue:
. Here’s an example:
var mapRow1 = [Int](count:10,repeatedValue:0) // [0,0,0,0,0,0,0,0,0,0,0]
Here we created a map for a game. If you were using this map for a game, you could place arrays within arrays to create a multidimensional array. You can use one array for each row and place that in one big array, like this:
var mapRow1 = [Int](count:10,repeatedValue:0)var cols = 10var rows = 10var map = [[Int]]()for row in 0..<rows { var newRow = [Int](count:cols, repeatedValue:0) map.append(newRow)}map ...
Get Learning Swift™ Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.