Roen의 iOS 개발로그

[Instance Method] split(maxSplits:omittingEmptySubsequences:whereSeparator:)

by Steady On
print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"​
print(line.split(maxSplits: 1, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "  I don\'t want realism. I want magic!"]"

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

컬렉션의 가능한 가장 긴 부분시퀀스를 지정된 조건자를 만족하는 요소를 포함하지 않고 순차적으로 리턴한다.

선언

func split(
    maxSplits: Int = Int.max,
    omittingEmptySubsequences: Bool = true,
    whereSeparator isSeparator: (Self.Element) throws -> Bool
) rethrows -> [Self.SubSequence]

매개변수

maxSplits

The maximum number of times to split the collection, or one less than the number of subsequences to return. If maxSplits + 1 subsequences are returned, the last one is a suffix of the original collection containing the remaining elements. maxSplits must be greater than or equal to zero. The default value is Int.max.

컬렉션을 나눌 최대 횟수 혹은 반환될 부분시퀀스의 최소 숫자. 만약 maxSplites + 1의 부분시퀀스가 반환되면, 가장 마지막 요소는 남은 요소들을 담은 원본 콜렉션의 접미사이다. maxSplits는 0보다 크거나 같아야한다. 기본값은 Int.max이다.

 

omittingEmptySubsequences

If false, an empty subsequence is returned in the result for each pair of consecutive elements satisfying the isSeparator predicate and for each element at the start or end of the collection satisfying the isSeparator predicate. The default value is true.

거짓인 경우, isSeparator 지정자를 만족하는 연속된 각 요소 쌍과 isSeparator 지정자를 만족하는 컬렉션의 시작 또는 끝에 있는 각 요소에 대해 결과에 빈 부분시퀀스가 반환됩니다. 기본값은 true입니다.

 

isSeparator

A closure that takes an element as an argument and returns a Boolean value indicating whether the collection should be split at that element.

요소를 인수로 받고 컬렉션이 해당 요소에서 분할되어야 하는지의 여부를 나타내는 불리언 값을 리턴하는 클로저

 

리턴 값

An array of subsequences, split from this collection’s elements.

분할된 컬렉션의 요소인 부분 시퀀스의 배열

 

논의

The resulting array consists of at most maxSplits + 1 subsequences. Elements that are used to split the sequence are not returned as part of any subsequence.

결과 배열은 최대 maxSplits + 1개의 부분시퀀스로 구성된다. 시퀀스를 분할하는데 사용되어진 요소들은 부분시퀀스의 일부로 반환되지 않는다.

 

The following examples show the effects of the maxSplits and omittingEmptySubsequences parameters when splitting a string using a closure that matches spaces. The first use of split returns each word that was originally separated by one or more spaces.

다음 예제는 공백과 일치하는 클로저를 사용해서 문자열을 나눌때 maxSplits와 omittingEmptySubsequences 매개변수의 효과를 보여준다. 분할의 첫번째 사용은 원래 하나 이상의 공백으로 분리된 각 단어를 반환한다.

let line = "BLANCHE:   I don't want realism. I want magic!"
print(line.split(whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

The second example passes 1 for the maxSplits parameter, so the original string is split just once, into two new strings.
두번째 예제는 maxSplits 매개변수를 1로 준다. 그래서 원본 문자열은 두개의 새 문자열로 한 번만 분할된다.

print(line.split(maxSplits: 1, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "  I don\'t want realism. I want magic!"]"

The final example passes false for the omittingEmptySubsequences parameter, so the returned array contains empty strings where spaces were repeated.
마지막 예제는 omittingEmptySubsequences 매개변수에 false를 준다. 그래서 반환된 배열은 공백이 반복된 빈 문자열을 포함한다.

print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

 

블로그의 정보

Roen의 iOS 개발로그

Steady On

활동하기