iOS 15 控制台中的消息:"Binding<String> action tried to update multiple times per frame."

Message in console on iOS 15: "Binding<String> action tried to update multiple times per frame."

我有以下代码:

struct Credential: Equatable {
    var username = ""
    var password = ""
}

enum Database {
    static var credential = Credential()
}

struct UsernamePasswordInput: View {
    @Binding var credential: Credential

    var body: some View {
        Group {
            TextField("Username", text: self.$credential.username)
            SecureField("password", text: self.$credential.password)
        }
        .textFieldStyle(RoundedBorderTextFieldStyle())
    }
}

struct Login: View {
    private var credential: Binding<Credential>

    var body: some View {
        UsernamePasswordInput(credential: self.credential)
    }

    init() {
        self.credential = Binding<Credential>(
            get: {
                Database.credential
            }, set: { newValue in
                Database.credential = newValue
            }
        )
    }
}

struct ContentView: View {
    var body: some View {
        Login()
    }
}

当您在 iOS 15 设备上 运行 来自 Xcode 13.0 的代码时,消息 Binding<String> action tried to update multiple times per frame. 会出现在 Xcode 控制台中。每当您在 TextFieldSecureField 中键入一个字符时,就会发生这种情况。该消息未出现在 iOS 13 或 14 台设备上。

我知道我可以通过使用 @StateObject 来解决它,但是上面的代码反映了一个更大项目的结构。我的问题是:Xcode 消息是否以某种方式表示存在问题?上面的代码在某种程度上主要是不正确的吗?

这是一个可能的安全解决方法 - 对每个文本字段使用内联分隔绑定:

struct UsernamePasswordInput: View {
    @Binding var credential: Credential

    var body: some View {
        Group {
            TextField("Username", text: Binding(
                get: { credential.username },
                set: { credential.username = [=10=] }
                ))
            SecureField("password", text: Binding(
                get: { credential.password },
                set: { credential.password = [=10=] }
                ))
        }
        .textFieldStyle(RoundedBorderTextFieldStyle())
    }
}

测试 Xcode 13 / iOS 15

backup