Language/Swift

Swift - Prefix

codinglearn 2020. 7. 13. 10:19
반응형

prefix(_:)

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

컬렉션의 초기 요소를 포함하는 지정된 최대 길이까지 하위 시퀀스를 반환한다.

 

func prefix(_ maxLength: Int) -> ArraySlice<Element>

 

let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"
반응형