Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a subArray from Swift 2.0

I keep trying to search for the proper way to get a sub array in Swift but I am missing something here. This code doesn't work because rowArray.append(row) throws an error that states.

Cannot convert value of type 'ArraySlice<Int>' to specified type '[Int]'

I can't figure out how to get an [Int] out of the main array or to convert ArraySlice<Int> to [Int]. I am guessing I am missing something simple but can't seem to find the answer from the docs.

var rowArray = [[Int]]()  var rangeStart = 0 let rangeLength = mapWidth  for var index = 0; index < mapHeight; ++index{     rangeStart = tileIDs.count - ((index + 1) * mapWidth )      let row : [Int] = tileIDs[rangeStart...rangeStart+rangeLength]     rowArray.append(row) } rowArray.append(row) 
like image 856
Skyler Lauren Avatar asked Oct 11 '15 00:10

Skyler Lauren


People also ask

How do you get a subarray from an array?

slice() The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. Basically, slice lets you select a subarray from an array.

Can a single element be a Subarray?

Answer is yes. In C, array is basically a pointer alongside with its length.

What is a Subarray JS?

subarray() is an inbuilt function in JavaScript which is used to return a part of the typedArray object. Syntax: typedarray.subarray(begin, end) Parameters: It accepts two parameters which are described below: begin: It specifies the index of the starting element from which the part of the given array to be started.


1 Answers

Convert ArraySlice<Int> to [Int] with Array():

let row : [Int] = Array(tileIDs[rangeStart...rangeStart+rangeLength]) 
like image 131
Eric Aya Avatar answered Sep 29 '22 13:09

Eric Aya