Add shortcut for getting element of array binding

This commit is contained in:
david-swift 2024-04-04 15:29:31 +02:00
parent 38e5028301
commit adbd1c2a00

View File

@ -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
}
}
}