forked from aparoksha/adwaita-swift
Add shortcut for getting element of array binding
This commit is contained in:
parent
38e5028301
commit
adbd1c2a00
@ -104,3 +104,20 @@ extension Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Binding where Value == [Any] {
|
||||||
|
|
||||||
|
/// 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: Int?, default defaultValue: Value.Element) -> Binding<Value.Element> {
|
||||||
|
.init {
|
||||||
|
wrappedValue[safe: index] ?? defaultValue
|
||||||
|
} set: { newValue in
|
||||||
|
wrappedValue[safe: index] = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user