2

我正在尝试将绑定传递给使用属性包装器创建的变量。当我将绑定传递给另一个视图时,我似乎失去了对基础类型的访问。在下面的示例代码中,我演示了我可以更新原始绑定值,但当我将其绑定传递给另一个视图时不能:

import SwiftUI

@propertyWrapper
struct BoundedNumber {
    private var number: Int
    private var minimum: Int
    private var maximum: Int
    
    init(wrappedValue: Int, minimum: Int, maximum: Int) {
        self.minimum = minimum
        self.maximum = maximum
        number = max(minimum, min(wrappedValue, maximum))
    }
    
    var wrappedValue: Int {
        get { return number }
        set { number = max(minimum, min(newValue, maximum)) }
    }
}

struct ContentView: View {
    @State @BoundedNumber(minimum: 0, maximum: 10) var firstNumber: Int = 1
    @State @BoundedNumber(minimum: 1, maximum: 5) var secondNumber: Int = 1
    
    var body: some View {
        VStack {
            HStack {
                Text("\(firstNumber)")
                UpdateButton($firstNumber, updateType: .decrement)
                UpdateButton($firstNumber, updateType: .increment)
            }
            HStack {
                Text("\(secondNumber)")
                UpdateButton($secondNumber, updateType: .decrement)
                UpdateButton($secondNumber, updateType: .increment)
            }
            Button { 
                firstNumber +=  1 // This compiles
            } label: { 
                Image(systemName: "plus")
            }
        }
    }
}

struct UpdateButton: View {
    @Binding var value: BoundedNumber
    let updateType: UpdateType

    init(_ value: Binding<BoundedNumber>, updateType: UpdateType) {
        _value = value
        self.updateType = updateType
    }

    enum UpdateType {
        case increment, decrement
    }

    var body: some View {
        Button { 
            value += updateType == .increment ? 1 : -1  
           // This gives a compiler error: 
           // Binary operator '+=' cannot be applied to operands of type 'BoundedNumber' and '_'
        } label: {
            Image(systemName: updateType == .increment ? "plus" : "minus")
        }
    }
}
4

1 回答 1

1

使用运算符添加以下扩展名,您的代码就可以工作了。使用 Xcode 12.4 / iOS 14.4 测试

extension BoundedNumber {
    static func +=(_ lhs: inout BoundedNumber, _ rhs: Int) {
        lhs.wrappedValue += rhs
    }
}
于 2021-03-06T12:41:03.037 回答