Swiftpack.co -  RougeWare/Swift-Safe-Collection-Access as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
RougeWare/Swift-Safe-Collection-Access
Ever wonder why Swift crashes if you access a collection the wrong way? Me too here's some extensions
.package(url: "https://github.com/RougeWare/Swift-Safe-Collection-Access.git", from: "2.2.1")

Tested on GitHub Actions

Swift 5 swift package manager 5.2 is supported Supports macOS, iOS, tvOS, watchOS, Linux, & Windows

Swift Safe Collection Access

Ever wonder why Swift crashes if you access a collection the wrong way? Especially an array? Me too here's some extensions. 🎉

collection[orNil:]

This subscript fails gracefully on invalid input by returning nil or refusing to mutate the collection. With valid input, it behaves precisely like Swift's builtin subscripts!


import SafeCollectionAccess



var first5Fibonacci = [1, 1, 2, 3, 5]

first5Fibonacci[orNil: 0]                       // Optional(1)
first5Fibonacci[orNil: 3] == first5Fibonacci[3] // true
first5Fibonacci[orNil: 999]                     // `nil`
first5Fibonacci[orNil: -1]                      // `nil`

first5Fibonacci[orNil: 2...4]  // Optional([2, 4])
first5Fibonacci[orNil: -2...2] // `nil`
first5Fibonacci[orNil: ..<42]  // `nil`


first5Fibonacci[orNil: 1]   = 42  // [1, 42, 2, 3, 5]
first5Fibonacci[orNil: 999] = -1  // [1, 42, 2, 3, 5]
first5Fibonacci[orNil: -1]  = 777 // [1, 42, 2, 3, 5]

first5Fibonacci[orNil: 0...2]  = [9, 5]    // [9, 5, 3, 5]
first5Fibonacci[orNil: -2...2] = [42]      // [9, 5, 3, 5]
first5Fibonacci[orNil: ..<42]  = [7, 7, 7] // [9, 5, 3, 5]

collection[clamping:]

This subscript fails gracefully on invalid input by returning or mutating the closest valid element (or, with empty collections, returning nil or refusing to mutate). With valid input, it behaves precisely like Swift's builtin subscripts!


import SafeCollectionAccess



var first5Fibonacci = [1, 1, 2, 3, 5]

first5Fibonacci[clamping: 0]                       // 1
first5Fibonacci[clamping: 3] == first5Fibonacci[3] // true
first5Fibonacci[clamping: 999]                     // 5
first5Fibonacci[clamping: -1]                      // 1


first5Fibonacci[clamping: 1]   = 42  // [1, 42, 2, 3, 5]
first5Fibonacci[clamping: 999] = -1  // [1, 42, 2, 3, -1]
first5Fibonacci[clamping: -1]  = 777 // [777, 42, 2, 3, -1]

GitHub

link
Stars: 0
Last commit: 3 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

2.2 - Mutation! 🎉
3 days ago

Finally, you can use this package to safely mutate collections, as well as accessing them safely as always!

All existing accessors now have mutating companions:

  • [orNil:] taking an index can now set that index, remove that element by setting it to nil, or do nothing if that index lies outside the collection
  • [orNil:] taking an index range can now set that range to the contents of a subsequence, remove the elements at that range by setting it to nil, or do nothing if that index range goes beyond the collection boundaries
  • [clamping:] taking an index can now set that index, remove that element by setting it to nil, or set the closest slot if the index lies outside the collection, or do nothing if the collection is empty

2.2.1

This patch fixes a crash when using this package to address strings and other collections which don't allow you to get an index if it's outside the collection

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API