SafeCollectionAccess

2.2.2

Ever wonder why Swift crashes if you access a collection the wrong way? Me too here's some extensions
RougeWare/Swift-Safe-Collection-Access

What's New

2.2 - Mutation! 🎉

2021-06-10T19:32:08Z

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

2.2.2

This patch adds a license to the codebase

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]

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 18 2024 19:19:35 GMT-0900 (Hawaii-Aleutian Daylight Time)