오늘은 Swift의 Stride에 대해
간단히 정리해보겠다.
> stride 관련 공식 문서 (through와 to) 보러 가기
https://developer.apple.com/documentation/swift/stride(from:to:by:)
stride(from:to:by:) | Apple Developer Documentation
Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.
developer.apple.com
https://developer.apple.com/documentation/swift/stride(from:through:by:)
stride(from:through:by:) | Apple Developer Documentation
Returns a sequence from a starting value toward, and possibly including, an end value, stepping by the specified amount.
developer.apple.com
먼저 stride(from: , through:, by: )는
from 매개변수 값부터 시작해서 through 값까지 by만큼 다음 값을 반복하며 사용한다.
가능하다면 through까지 포함하는게 포인트이다.
for i in stride(from: 2, through: 16, by: 2) {
print(i, terminator: ", ")
}
// 2, 4, 6, 8, 10, 12, 14, 16
stride(from: , to:, by: )는
from 매개변수 값부터 시작해서 to 값까지 by만큼 다음 값을 반복하며 사용한다.
이때 끝 값인 to는 포함하지 않는다.
for i in stride(from: 2, to: 16, by: 2) {
print(i, terminator: ", ")
}
// 2, 4, 6, 8, 10, 12, 14
정리하자면
stride(from: , through:, by: )는 끝 값인 through까지 포함하여 반복하고,
stride(from: , to:, by: )는 to 값은 포함하지 않으며 그 전까지 반복한다.
'TIL' 카테고리의 다른 글
[TIL] Swift - 문자열 처리 components vs. split (0) | 2024.08.17 |
---|---|
[TIL] SwiftUI - Extra argument in call 오류 (0) | 2023.04.18 |