SwiftUI Xcode 11 beta 5/6:没有更多上下文的表达式类型不明确 [英] SwiftUI Xcode 11 beta 5 / 6: Type of expression is ambiguous without more context
本文介绍了SwiftUI Xcode 11 beta 5/6:没有更多上下文的表达式类型不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从 beta 5 开始,我的项目将不再编译.对于该按钮的文本,错误是 没有更多上下文的表达式类型不明确"
.我认为它可能会在 beta 6 中修复,但事实并非如此.macOS 也更新到 Catalina beta 7.
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 be fixed with beta 6 maybe, but it wasn't. macOS is also updated to 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
}
}
推荐答案
在您更新答案后,我发现需要进行一些更改:
After your update in your answer, I see some changes needed:
- 使用
ObservableObject
(无需使用Combine.ObservableObject
) - 缺少功能
scanPassport
,我添加了一个假的. willChange
不再存在,现在是objectWillChange
.它是为您自动合成的.
- Use
ObservableObject
(no need to useCombine.ObservableObject
) - Missing function
scanPassport
, I added a bogus one. willChange
no longer exists, it is nowobjectWillChange
. 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
}
}
这篇关于SwiftUI Xcode 11 beta 5/6:没有更多上下文的表达式类型不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文