从beta 5开始,我的项目将不再编译。该按钮的文本错误为"Type of expression is ambiguous without more context"
。
我以为可能会在beta 6中修复它,但事实并非如此。 macOS也已更新为Catalina beta 7。
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") // <- "Type of expression is ambiguous without more context"
.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($passportDetails.mrz, placeholder: Text("MRZ"))
}
}
}
func scanPassport( mrzKey: String ) {
//do stuff with mrzKey
}
}
答案 0 :(得分:1)
更新您的答案后,我发现需要进行一些更改:
ObservableObject
(无需使用Combine.ObservableObject
)scanPassport
,我添加了一个伪造的功能。willChange
不再存在,现在为objectWillChange
。它会自动为您合成。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
}
}