https://github.com/efprefix/efsafearray
EFSafeArray is an extension to make array subscript safe, support iOS, macOS, watchOS and tvOS.
https://github.com/efprefix/efsafearray
array auto nil range safe swift
Last synced: about 1 year ago
JSON representation
EFSafeArray is an extension to make array subscript safe, support iOS, macOS, watchOS and tvOS.
- Host: GitHub
- URL: https://github.com/efprefix/efsafearray
- Owner: EFPrefix
- License: mit
- Created: 2017-07-28T15:41:03.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-02-14T19:48:57.000Z (over 1 year ago)
- Last Synced: 2025-04-04T23:43:53.233Z (about 1 year ago)
- Topics: array, auto, nil, range, safe, swift
- Language: Swift
- Homepage: https://swiftpackageindex.com/EFPrefix/EFSafeArray
- Size: 166 KB
- Stars: 16
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

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`.
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
- Xcode 16+
- Swift 6.0+
## Installation
### CocoaPods
EFSafeArray is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:
```ruby
pod 'EFSafeArray'
```
### Swift Package Manager
The [Swift Package Manager](https://swift.org/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`.
```swift
dependencies: [
.package(url: "https://github.com/EFPrefix/EFSafeArray.git", .upToNextMinor(from: "6.0.0.0"))
]
```
## Use
```swift
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? = [1, 2, 3, 4, 5, 6]
let oooo = list[(-1...12)~] // oooo: ArraySlice? = [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]
```
## Author
EyreFree, eyrefree@eyrefree.org
## License

EFSafeArray is available under the MIT license. See the LICENSE file for more info.