IdentifiedArray
public struct IdentifiedArray<ID, Element>: MutableCollection, RandomAccessCollection
where ID: Hashable
extension IdentifiedArray: CustomDebugStringConvertible
extension IdentifiedArray: CustomReflectable
extension IdentifiedArray: CustomStringConvertible
extension IdentifiedArray: Decodable where Element: Decodable & Identifiable, ID == Element.ID
extension IdentifiedArray: Encodable where Element: Encodable
extension IdentifiedArray: Equatable where Element: Equatable
extension IdentifiedArray: Hashable where Element: Hashable
extension IdentifiedArray: ExpressibleByArrayLiteral where Element: Identifiable, ID == Element.ID
extension IdentifiedArray: RangeReplaceableCollection
where Element: Identifiable, ID == Element.ID
An array of elements that can be identified by a given key path.
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:)
)
}
}
}
-
A key path to a value that identifies an element.
Declaration
Swift
public let id: KeyPath<Element, ID>
-
A raw array of each element’s identifier.
Declaration
Swift
public private(set) var ids: [ID] { get }
-
A raw array of the underlying elements.
Declaration
Swift
public var elements: [Element] { get }
-
Initializes an identified array with a sequence of elements and a key path to an element’s identifier.
Declaration
Swift
public init<S>(_ elements: S, id: KeyPath<Element, ID>) where Element == S.Element, S : Sequence
Parameters
elements
A sequence of elements.
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.
Declaration
Swift
public init(id: KeyPath<Element, ID>)
Parameters
id
A key path to a value that identifies an element.
-
Declaration
Swift
public var startIndex: Int { get }
-
Declaration
Swift
public var endIndex: Int { get }
-
Declaration
Swift
public func index(after i: Int) -> Int
-
Declaration
Swift
public func index(before i: Int) -> Int
-
Declaration
Swift
public subscript(position: Int) -> Element { get set }
-
Direct access to an element by its identifier.
Declaration
Swift
public subscript(id id: ID) -> Element? { get set }
Parameters
id
The identifier of element to access. Must be a valid identifier for an element of the array and will not insert elements that are not already in the array, or remove elements when passed
nil
. Useappend
orinsert(_:at:)
to insert elements. Useremove(id:)
to remove an element by its identifier.Return Value
The element.
-
-
Declaration
Swift
public mutating func insert(_ newElement: Element, at i: Int)
-
Declaration
Swift
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.
Declaration
Swift
@discardableResult public mutating func remove(id: ID) -> Element
Parameters
id
The identifier of the element to remove.
Return Value
The removed element.
-
Declaration
Swift
@discardableResult public mutating func remove(at position: Int) -> Element
-
Declaration
Swift
public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
-
Undocumented
Declaration
Swift
public mutating func remove(atOffsets offsets: IndexSet)
-
Undocumented
Declaration
Swift
public mutating func move(fromOffsets source: IndexSet, toOffset destination: Int)
-
Undocumented
Declaration
Swift
public mutating func sort(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows
-
Declaration
Swift
public var debugDescription: String { get }
-
Declaration
Swift
public var customMirror: Mirror { get }
-
Declaration
Swift
public var description: String { get }
-
Declaration
Swift
public init(from decoder: Decoder) throws
-
Declaration
Swift
public func encode(to encoder: Encoder) throws
-
Undocumented
Declaration
Swift
public mutating func sort()
-
Declaration
Swift
public init(arrayLiteral elements: Element...)
-
Declaration
Swift
public init<S>(_ elements: S) where Element == S.Element, S : Sequence
-
Declaration
Swift
public init()
-
Declaration
Swift
public mutating func replaceSubrange<C, R>(_ subrange: R, with newElements: C) where C: Collection, R: RangeExpression, Element == C.Element, Index == R.Bound