Swift Get First N Elements Of Array

[Solved] Swift Get First N Elements Of Array | Swift - Code Explorer | yomemimo.com
Question : swift get first n elements of array

Answered by : doge-amazedo

// the neatest way to get the first N elements of a Swift array is using
// prefix(_ maxLength: Int):
let someArray = [1, 2, 3, 4, 5, 6, 7]
let first5 = someArray.prefix(5) // 1, 2, 3, 4, 5

Source : https://stackoverflow.com/questions/28527797/how-to-return-first-5-objects-of-array-in-swift | Last Update : Mon, 12 Jul 21

Answers related to swift get first n elements of array

Code Explorer Popular Question For Swift