Array - leacode/SwiftWings GitHub Wiki
Source: Sources/Extensions/Foundation/Array
Tests: Tests/Extensions/Array
SwiftWings ships two tiny helpers on Array when working with Foundation data transformations.
- Adds
Array<UInt8>.datato turn any byte buffer intoDatawithout manual initializers. - Useful when bridging between crypto/hash primitives (which often surface byte arrays) and Foundation APIs that expect
Data.
-
subscript(guard:)safely returnsnilinstead of crashing when an index is out of bounds. -
reduct(_:)runs a custom reducer only if the array contains at least one element, mirroring the behavior exercised inArray+SubscriptTests(e.g.[1,2,3].reduct(+) == 6).
let bytes: [UInt8] = [0xF, 0x0, 0x0, 0xD]
print(bytes.data as NSData) // <0f00000d>
let letters = ["a", "b"]
print(letters[guard: 5] as Any) // nil
let total = [1, 2, 3, 4].reduct(+) // Optional(10)