Swift Array - 인덱스가 존재하는지 확인합니다.
Swift에서는 치명적인 오류가 발생하지 않고 배열에 인덱스가 존재하는지 확인할 수 있는 방법이 있습니까?
난 내가 이런 걸 할 수 있길 바랬어
let arr: [String] = ["foo", "bar"]
let str: String? = arr[1]
if let str2 = arr[2] as String? {
// this wouldn't run
println(str2)
} else {
// this would be run
}
하지만 난 이해한다
치명적인 오류: 어레이 인덱스가 범위를 벗어났습니다.
Swift의 우아한 방법:
let isIndexValid = array.indices.contains(index)
유형 확장자:
extension Collection {
subscript(optional i: Index) -> Iterator.Element? {
return self.indices.contains(i) ? self[i] : nil
}
}
이 옵션을 사용하면 인덱스에 옵션 키워드를 추가할 때 옵션 값이 반환됩니다. 즉, 인덱스가 범위를 벗어나도 프로그램이 충돌하지 않습니다.이 예에서는 다음과 같습니다.
let arr = ["foo", "bar"]
let str1 = arr[optional: 1] // --> str1 is now Optional("bar")
if let str2 = arr[optional: 2] {
print(str2) // --> this still wouldn't run
} else {
print("No string found at that index") // --> this would be printed
}
인덱스가 어레이 크기보다 작은지 확인합니다.
if 2 < arr.count {
...
} else {
...
}
확장설탕을 첨가한다.
extension Collection {
subscript(safe index: Index) -> Iterator.Element? {
guard indices.contains(index) else { return nil }
return self[index]
}
}
if let item = ["a", "b", "c", "d"][safe: 3] { print(item) } // Output: "d"
// or with guard:
guard let anotherItem = ["a", "b", "c", "d"][safe: 3] else { return }
print(anotherItem) // "d"
★★★★★★★★★★의 if let
arrays(와 한 스타일 코딩)
최선의 방법이야.
let reqIndex = array.indices.contains(index)
print(reqIndex)
Swift 4 확장:
나는 방법이 더 좋다.
// MARK: - Extension Collection
extension Collection {
/// Get at index object
///
/// - Parameter index: Index of object
/// - Returns: Element at index or nil
func get(at index: Index) -> Iterator.Element? {
return self.indices.contains(index) ? self[index] : nil
}
}
@Benno Kress님 덕분입니다.
보다 안전한 방법으로 어레이 크기를 확인하고 3진수 조건을 사용할 수 있습니다.
if let str2 = (arr.count > 2 ? arr[2] : nil) as String?
extension Array {
func isValidIndex(_ index : Int) -> Bool {
return index < self.count
}
}
let array = ["a","b","c","d"]
func testArrayIndex(_ index : Int) {
guard array.isValidIndex(index) else {
print("Handle array index Out of bounds here")
return
}
}
indexOutOfBounds를 처리하는 것이 제 업무입니다.
Swift 4 및 5 확장:
저는 이것이 가장 안전한 해결책이라고 생각합니다.
public extension MutableCollection {
subscript(safe index: Index) -> Element? {
get {
return indices.contains(index) ? self[index] : nil
}
set(newValue) {
if let newValue = newValue, indices.contains(index) {
self[index] = newValue
}
}
}
}
예:
let array = ["foo", "bar"]
if let str = array[safe: 1] {
print(str) // "bar"
} else {
print("index out of range")
}
배열 인덱스가 존재하는지 확인:
확장설탕을 첨가하지 않으려면 이 방법이 좋습니다.
let arr = [1,2,3]
if let fourthItem = (3 < arr.count ? arr[3] : nil ) {
Swift.print("fourthItem: \(fourthItem)")
}else if let thirdItem = (2 < arr.count ? arr[2] : nil) {
Swift.print("thirdItem: \(thirdItem)")
}
//Output: thirdItem: 3
이 기능은 코드베이스(공통 조작을 반복할 때의 코드 냄새) 내의 여러 곳에서 필요할 수 있기 때문에 기존의 답변은 더욱 개선될 수 있다고 생각합니다.그래서 왜 이 방법을 검토했는지를 추론하면서 나만의 구현을 추가하려고 했습니다(효율성은 좋은 API 설계의 중요한 부분이며 가독성에 크게 영향을 주지 않는 한 가능하면 선호해야 합니다).타입 자체에 대한 메서드로 오브젝트 지향 설계를 실시하는 것 외에, Protocol Extensions도 훌륭하고, 기존의 답변도 한층 더 스위프티하게 할 수 있다고 생각합니다.사용하지 않는 코드를 만들지 않기 때문에 확장자를 제한하는 것이 좋습니다.코드를 깔끔하고 확장 가능하게 하면 유지보수가 쉬워지는 경우가 많지만 단점이 있습니다(최초 생각한 것은 간결함입니다).
따라서 재사용을 위해 확장 아이디어만 사용하고 싶지만contains
위에서 언급한 방법을 사용하여 이 답변을 재작업할 수 있습니다.나는 이 대답을 다양한 용도에 맞게 유연하게 하려고 노력해왔다.
TL;DR
보다 효율적인 알고리즘(공간 및 시간)을 사용하고 일반 제약 조건이 있는 프로토콜 확장을 사용하여 확장할 수 있습니다.
extension Collection where Element: Numeric { // Constrain only to numerical collections i.e Int, CGFloat, Double and NSNumber
func isIndexValid(index: Index) -> Bool {
return self.endIndex > index && self.startIndex <= index
}
}
// Usage
let checkOne = digits.isIndexValid(index: index)
let checkTwo = [1,2,3].isIndexValid(index: 2)
딥 다이브
효율성.
@Manuel의 답변은 매우 우아하지만, 한층 더 간접적인 표현을 사용하고 있다(여기를 참조).인덱스 속성은 다음과 같이 동작합니다.CountableRange<Int>
로 만든startIndex
★★★★★★★★★★★★★★★★★」endIndex
공간가 매우 )String
, 간, 간, ,, ,, ,, ,, ,, the, the, the, the, the, the, the, the, complex, complex, complex, complex, complex, the, the, the, the, the의 비교와 비슷해야 합니다.endIndex
★★★★★★★★★★★★★★★★★」startIndex
= 2인 에도 N =입니다.contains(_:)
(N)는 O입니다.Collection
s)Range
는 시작 및 종료 인덱스에 대한 두 가지 속성만 있습니다).
공간 및 시간의 복잡성, 확장성 및 약간 긴 코드를 최대한 활용하려면 다음을 사용하는 것이 좋습니다.
extension Collection {
func isIndexValid(index: Index) -> Bool {
return self.endIndex > index && self.startIndex <= index
}
}
에서는 제가 .startIndex
- 0 을 - 을 때 - 을 때 - 을 때마침 0 을 - 을 수 있다.ArraySlice
기타 " " "SubSequence
유형입니다. 이것이 해결책을 게시하는 또 다른 동기였습니다.
사용 예:
let check = digits.isIndexValid(index: index)
★★★의 Collection
것을 어렵습니다.Index
Swift의 .associatedtype Index
Collection
한 - 수 .Collection.Index
것)startIndex
를 참조해 주세요.
하면, 생으로 사용하는 입니다.Int
인덱스를 참조해 주세요.Array
s를 가 있는 입니다.Array
이치노따라서 방법을 더 적은 수의 구조로 제한하는 것이 좋습니다.
메서드 범위 제한
은 모든 을 망라하여 수 있다는 을 알 수 있습니다.Collection
은, 을 「( 「)」, 「」로 할 수 .Array
앱의 를 들어 추가된 앱이 필요 없는 경우)에.String
메서드(필요없기 때문에)를 선택합니다.
extension Array {
func isIndexValid(index: Index) -> Bool {
return self.endIndex > index && self.startIndex <= index
}
}
★★★의 Array
s, s, s는 Index
다음 중 하나:
let check = [1,2,3].isIndexValid(index: 2)
해 주세요.그 밖에도 가 있습니다. 그 밖에도 여러 가지 유형이 있습니다.Collection
" " " 등,LazyCollection
조건 s)을 도 있습니다. 일반적인 제약조건을 사용할 수도 있습니다.하다
extension Collection where Element: Numeric {
func isIndexValid(index: Index) -> Bool {
return self.endIndex > index && self.startIndex <= index
}
}
에 의해, 가 「」, 「」로 됩니다.Numeric
Collection
단, s, ,를 사용할 수 .String
명시적으로도, 반대로도.다시 말하지만 코드 크리프를 피하기 위해 특별히 사용하는 것으로 함수를 제한하는 것이 좋습니다.
여러 모듈에 걸친 메서드 참조
컴파일러는 제네릭이 일반적으로 문제가 되지 않도록 하기 위해 이미 여러 최적화를 적용하고 있지만 코드가 다른 모듈에서 호출될 때는 이러한 최적화가 적용되지 않습니다.에는 ★★★★★★★★★★★★★를 사용합니다.@inlinable
는 프레임워크의 바이너리 크기를 늘리는 대신 흥미로운 퍼포먼스를 향상시킬 수 있습니다.일반적으로 성능 향상에 관심이 있고 SOC를 개선하기 위해 함수를 별도의 Xcode 타겟에 캡슐화하려는 경우 다음을 시도해 볼 수 있습니다.
extension Collection where Element: Numeric {
// Add this signature to the public header of the extensions module as well.
@inlinable public func isIndexValid(index: Index) -> Bool {
return self.endIndex > index && self.startIndex <= index
}
}
모듈러형 코드베이스 구조를 시험해 볼 것을 권장합니다.일반 운용 프로젝트에서 단일 책임(및 SOLID)을 확보하는 데 도움이 된다고 생각합니다.여기서의 순서를 실행해 보면, 이 최적화를 사용할 수 있습니다(소외적으로).컴파일러 조작은 콜 사이트마다 1줄의 코드만 추가할 수 있기 때문에 이 함수에 Atribut을 사용하는 것은 문제가 없지만 메서드가 콜 스택에 추가되지 않기 때문에 성능을 향상시킬 수 있습니다(따라서 추적할 필요가 없습니다).이 기능은 가장 빠른 속도를 필요로 하고 작은 이진수 크기가 증가해도 상관 없는 경우에 유용합니다.(-: 또는 새로운 것을 시험해 보겠습니다.XCFramework
, <13>의 에 주의해 주세요). (「 」 <iOS 13 > 「 ObjC 」 。
스위프트의 모든 프로젝트에 이 확장을 추가해야 한다고 생각합니다.
extension Collection {
@inlinable func isValid(position: Self.Index) -> Bool {
return (startIndex..<endIndex) ~= position
}
@inlinable func isValid(bounds: Range<Self.Index>) -> Bool {
return (startIndex..<endIndex) ~= bounds.upperBound
}
@inlinable subscript(safe position: Self.Index) -> Self.Element? {
guard isValid(position: position) else { return nil }
return self[position]
}
@inlinable subscript(safe bounds: Range<Self.Index>) -> Self.SubSequence? {
guard isValid(bounds: bounds) else { return nil }
return self[bounds]
}
}
extension MutableCollection {
@inlinable subscript(safe position: Self.Index) -> Self.Element? {
get {
guard isValid(position: position) else { return nil }
return self[position]
}
set {
guard isValid(position: position), let newValue = newValue else { return }
self[position] = newValue
}
}
@inlinable subscript(safe bounds: Range<Self.Index>) -> Self.SubSequence? {
get {
guard isValid(bounds: bounds) else { return nil }
return self[bounds]
}
set {
guard isValid(bounds: bounds), let newValue = newValue else { return }
self[bounds] = newValue
}
}
}
해 주세요.isValid(position:)
★★★★★★★★★★★★★★★★★」isValid(bounds:)
이 O(1)
의 대답과는 (다음에)를 합니다.contains(_:)
O(n)
사용 예:
let arr = ["a","b"]
print(arr[safe: 2] ?? "nil") // output: nil
print(arr[safe: 1..<2] ?? "nil") // output: nil
var arr2 = ["a", "b"]
arr2[safe: 2] = "c"
print(arr2[safe: 2] ?? "nil") // output: nil
arr2[safe: 1..<2] = ["c","d"]
print(arr[safe: 1..<2] ?? "nil") // output: nil
언급URL : https://stackoverflow.com/questions/25976909/swift-array-check-if-an-index-exists
'programing' 카테고리의 다른 글
Swift에서 앱이 장치 또는 시뮬레이터용으로 구축되어 있는지 여부를 감지하는 방법 (0) | 2023.04.14 |
---|---|
VBA 또는 매크로 없이 Excel을 루프인하려면 어떻게 해야 합니까? (0) | 2023.04.14 |
git 마크를 삭제한 파일과 새로운 파일을 파일 이동으로 만드는 방법 (0) | 2023.04.14 |
병합된 Git 브랜치를 모두 삭제하려면 어떻게 해야 하나요? (0) | 2023.04.14 |
Bash에서 테이블 열을 정렬하려면 어떻게 해야 합니까? (0) | 2023.04.14 |