forked from aparoksha/adwaita-swift
Add support for ID subscripts
This commit is contained in:
parent
50b22115b0
commit
8cba3242f5
@ -111,3 +111,38 @@ public struct Binding<Value> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Binding where Value: MutableCollection {
|
||||||
|
|
||||||
|
/// Get a child at a certain index of the array as a binding.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - index: The child's index.
|
||||||
|
/// - defaultValue: The value used if the index is out of range does not exist.
|
||||||
|
/// - Returns: The child as a binding.
|
||||||
|
public subscript(safe index: Value.Index?, default defaultValue: Value.Element) -> Binding<Value.Element> {
|
||||||
|
.init {
|
||||||
|
if let index, wrappedValue.indices.contains(index) {
|
||||||
|
return wrappedValue[index] ?? defaultValue
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
} set: { newValue in
|
||||||
|
if let index, wrappedValue.indices.contains(index) {
|
||||||
|
wrappedValue[index] = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Binding where Value: MutableCollection, Value.Element: Identifiable {
|
||||||
|
|
||||||
|
/// Get a child of the array with a certain id as a binding.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - id: The child's id.
|
||||||
|
/// - defaultValue: The value used if the index is out of range does not exist.
|
||||||
|
/// - Returns: The child as a binding.
|
||||||
|
public subscript(id id: Value.Element.ID?, default defaultValue: Value.Element) -> Binding<Value.Element> {
|
||||||
|
self[safe: wrappedValue.firstIndex { $0.id == id }, default: defaultValue]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -105,24 +105,19 @@ extension Array {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Binding where Value: MutableCollection {
|
extension Array where Element: Identifiable {
|
||||||
|
|
||||||
/// Get a child at a certain index of the array as a binding.
|
/// Accesses the element with a certain id safely.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - index: The child's index.
|
/// - id: The child's id.
|
||||||
/// - defaultValue: The value used if the index is out of range does not exist.
|
///
|
||||||
/// - Returns: The child as a binding.
|
/// Access and set elements the safe way:
|
||||||
public subscript(safe index: Value.Index?, default defaultValue: Value.Element) -> Binding<Value.Element> {
|
/// ```swift
|
||||||
.init {
|
/// var array = ["Hello", "World"]
|
||||||
if let index, wrappedValue.indices.contains(index) {
|
/// print(array[safe: 2] ?? "Out of range")
|
||||||
return wrappedValue[index] ?? defaultValue
|
/// ```
|
||||||
}
|
public subscript(id id: Element.ID) -> Element? {
|
||||||
return defaultValue
|
self[safe: firstIndex { $0.id == id }]
|
||||||
} set: { newValue in
|
|
||||||
if let index, wrappedValue.indices.contains(index) {
|
|
||||||
wrappedValue[index] = newValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user