IdentifiedArray - mbrandonw/swift-composable-architecture GitHub Wiki
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:)
)
}
}
}
CustomDebugStringConvertible
, CustomReflectable
, CustomStringConvertible
, Decodable
, Encodable
, ExpressibleByArrayLiteral
, MutableCollection
, RandomAccessCollection
, RangeReplaceableCollection
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
- elements: - elements: A sequence of elements.
- id: - id: A key path to a value that identifies an element.
Initializes an empty identified array with a key path to an element's identifier.
public init(id: KeyPath<Element, ID>)
- id: - id: A key path to a value that identifies an element.
A key path to a value that identifies an element.
let id: KeyPath<Element, ID>
A raw array of each element's identifier.
var ids: [ID]
A raw array of the underlying elements.
var elements: [Element]
var dictionary: [ID: Element]
var startIndex: Int
var endIndex: Int
var debugDescription: String
var customMirror: Mirror
var description: String
public func index(after i: Int) -> Int
public func index(before i: Int) -> Int
public mutating func insert(_ newElement: Element, at i: Int)
public mutating func insert<C>(contentsOf newElements: C, at i: Int) where C: Collection, Element == C.Element
Removes and returns the element with the specified identifier.
@discardableResult public mutating func remove(id: ID) -> Element
- id: - id: The identifier of the element to remove.
The removed element.
@discardableResult public mutating func remove(at position: Int) -> Element
public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
public mutating func remove(atOffsets offsets: IndexSet)
public mutating func move(fromOffsets source: IndexSet, toOffset destination: Int)
public mutating func sort(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows