SwiftUI Xcode 11 beta 5 / 6: Type of expression is ambiguous without more context

后端 未结 1 1798
傲寒
傲寒 2021-01-07 12:58

Since beta 5 my Project won\'t compile anymore. The error is \"Type of expression is ambiguous without more context\"for that Button\'s Text. I thought it would

1条回答
  •  执念已碎
    2021-01-07 13:49

    After your update in your answer, I see some changes needed:

    • Use ObservableObject (no need to use Combine.ObservableObject)
    • Missing function scanPassport, I added a bogus one.
    • willChange no longer exists, it is now objectWillChange. And it is autosynthesize for you.
    import SwiftUI
    
    class ClPassport : ObservableObject , Identifiable {
    
    
        @Published var mrz : String =  "" //{ didSet { update() } }
        var isValid : Bool {
            return true
        }
    
        func update() {
            objectWillChange.send()
        }
    
        func getMRZKey() -> String {
         return ""
        }
    }
    
    
    struct ContentView : View {
    
        @ObservedObject var passportDetails = ClPassport()
          var body: some View {
    
             ZStack{
               VStack(alignment: .leading){
                 HStack{
                      Spacer()
                      Button(action: {
                          self.scanPassport( mrzKey: self.passportDetails.getMRZKey() )
                        }) {
                            Text("Read Chip")
                                .font(.largeTitle)
                            .foregroundColor(passportDetails.isValid ? .primary : Color.secondary.opacity(0.25))
                                .padding()
                            }.padding()
                            .background(Color.white.opacity(passportDetails.isValid ? 1 : 0.5))
                            .cornerRadius(15)
                            .padding()
                            .disabled( !passportDetails.isValid )
                       Spacer()
                  }
                TextField("MRZ", text: $passportDetails.mrz)
                }
              }
            }
    
     func scanPassport( mrzKey: String ) {
       //do stuff with mrzKey
     }
    }
    

    0 讨论(0)
提交回复
热议问题