FirstNonNil<T>

// 2020.10.15๏ผš

import SwiftUI

// ๐Ÿ“ฆ FirstNonNil<T>
public struct FirstNonNil<T>: PreferenceKey {
    public typealias Value = T?
    // default value
    public static var defaultValue: Value { nil }
    // combine values from different child views
    public static func reduce(value: inout Value, nextValue: () -> Value) {
        value = value ?? nextValue()   // nil or first non-nil value
    }
}

Last updated