EFSafeArray is an extension to make array subscript safe, nil will be return instead of crash if index is out of range, it works on iOS
, macOS
, watchOS
and tvOS
.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Version | Needs |
---|---|
0.x | XCode 8.0+ Swift 3.0+ iOS 8.0+ |
4.x | XCode 9.0+ Swift 4.0+ iOS 8.0+ |
5.x | XCode 10.2+ Swift 5.0+ iOS 8.0+ / macOS 10.11+ / tvOS 9.0+ / watchOS 2.0+ |
EFSafeArray is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EFSafeArray'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the Swift compiler.
Once you have your Swift package set up, adding EFSafeArray as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/EFPrefix/EFSafeArray.git", .upToNextMinor(from: "5.1.4"))
]
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
// Get Index
let xxx = list[0] // xxx: Int = 1
let zzz = list[0~] // zzz: Int? = 1
let yyy = list[10~] // yyy: Int? = nil
// Set Index
list[0] = 0 // list = [0, 2, 3, 4, 5, 6, 7, 8, 9, 0]
list[0~] = 1 // list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
list[10~] = 10 // list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
// Get Bounds
let iiii = list[(0...5)~] // iiii: ArraySlice<Int>? = [1, 2, 3, 4, 5, 6]
let oooo = list[(-1...12)~] // oooo: ArraySlice<Int>? = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
// Set Bounds
list[(0...5)~] = [1] // list = [1, 7, 8, 9, 0]
list[(-1...12)~] = [2, 3, 4, 5] // list = [2, 3, 4, 5]
EyreFree, eyrefree@eyrefree.org
EFSafeArray is available under the MIT license. See the LICENSE file for more info.