IdentifiedArray - mbrandonw/swift-composable-architecture GitHub Wiki

IdentifiedArray

An array of elements that can be identified by a given key path.

public struct IdentifiedArray<ID, Element>: MutableCollection, RandomAccessCollection where ID: Hashable

A useful container of state that is intended to interface with SwiftUI.ForEach. For example, your application may model a counter in an identifiable fashion:

struct CounterState: Identifiable {
  let id: UUID
  var count = 0
}
enum CounterAction { case incr, decr }
let counterReducer = Reducer<CounterState, CounterAction, Void> { ... }

This domain can be pulled back to a larger domain with the forEach method:

struct AppState { var counters = IdentifiedArray<Int>(id: \.self) }
enum AppAction { case counter(id: UUID, action: CounterAction) }
let appReducer = counterReducer.forEach(
  state: \AppState.counters,
  action: /AppAction.counter(id:action:),
  environment: { $0 }
)

And then SwiftUI can work with this array of identified elements in a list view:

struct AppView: View {
  let store: Store<AppState, AppAction>

  var body: some View {
    List {
      ForEachStore(
        self.store.scope(state: \.counters, action: AppAction.counter(id:action))
        content: CounterView.init(store:)
      )
    }
  }
}

Inheritance

CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Decodable, Encodable, ExpressibleByArrayLiteral, MutableCollection, RandomAccessCollection, RangeReplaceableCollection

Initializers

init(_:id:)

Initializes an identified array with a sequence of elements and a key path to an element's identifier.

public init<S>(_ elements: S, id: KeyPath<Element, ID>) where S: Sequence, S.Element == Element

Parameters

  • elements: - elements: A sequence of elements.
  • id: - id: A key path to a value that identifies an element.

init(id:)

Initializes an empty identified array with a key path to an element's identifier.

public init(id: KeyPath<Element, ID>)

Parameters

  • id: - id: A key path to a value that identifies an element.

Properties

id

A key path to a value that identifies an element.

let id: KeyPath<Element, ID>

ids

A raw array of each element's identifier.

var ids: [ID]

elements

A raw array of the underlying elements.

var elements: [Element]

dictionary

var dictionary: [ID: Element]

startIndex

var startIndex: Int

endIndex

var endIndex: Int

debugDescription

var debugDescription: String

customMirror

var customMirror: Mirror

description

var description: String

Methods

index(after:)

public func index(after i: Int) -> Int

index(before:)

public func index(before i: Int) -> Int

insert(_:at:)

public mutating func insert(_ newElement: Element, at i: Int)

insert(contentsOf:at:)

public mutating func insert<C>(contentsOf newElements: C, at i: Int) where C: Collection, Element == C.Element

remove(id:)

Removes and returns the element with the specified identifier.

@discardableResult public mutating func remove(id: ID) -> Element

Parameters

  • id: - id: The identifier of the element to remove.

Returns

The removed element.

remove(at:)

@discardableResult public mutating func remove(at position: Int) -> Element

removeAll(where:)

public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows

remove(atOffsets:)

public mutating func remove(atOffsets offsets: IndexSet)

move(fromOffsets:toOffset:)

public mutating func move(fromOffsets source: IndexSet, toOffset destination: Int)

sort(by:)

public mutating func sort(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows
⚠️ **GitHub.com Fallback** ⚠️