我正在创建我的第一个 SwiftUI 项目。一切都很顺利,直到我向项目中添加了一个新类(一个 XML 解析器)。现在,当我尝试调用新类时,出现错误“使用未解析的标识符”。编译器似乎不知道新类。
下面的代码被大大简化了。希望我已经提供了足够的信息来传达这个想法:
import SwiftUI
struct SelectPumpView: View {
@Environment(\.presentationMode) var presentation
@State var sources:[FuelSource] = TestData.sources()
// @State var fuelParser = PtXmlParser()
var body: some View {
NavigationView {
GeometryReader { geometry in
VStack {
VStack {
SelectPumpViewHeaderImage()
SelectPumpViewHeaderText()
TableColumnHeaderView()
} .frame(width: geometry.size.width, height: geometry.size.height/2, alignment: .top)
List (self.sources) { row in
// create a single row
HStack {
VStack (alignment: .leading ) {
Text("\(row.sourceID ?? "--")").font(.body)
.frame(width: geometry.size.width/3 )
}
VStack {
Text("\(row.kFactor ?? "--")").font(.body)
.frame(width: geometry.size.width/8 )
}
VStack {
Text("\(row.fuelType ?? "--")").font(.body)
.frame(width: geometry.size.width/8 )
}
VStack (alignment: .trailing ) {
Text("\(row.units ?? "--")").font(.body)
.frame(width: geometry.size.width/4 )
}
}
}
Button(action: {
// for debug only
for i in 0 ..< self.sources.count {
self.sources[i].debugPrint()
}
}) {
DisplayParseButton()
}
} // end outer VStack
.frame(width: geometry.size.width, height: geometry.size.height)
} // end GeometryView
} // end NavigationView
} // end View
}
解析器类...
import Foundation
import SwiftUI
class PtXmlParser: NSObject, XMLParserDelegate {
override init() {
super.init()
if let path = Bundle.main.url(forResource: "FogTypes", withExtension: "xml") {
if let parser = XMLParser(contentsOf: path) {
parser.delegate = self
parser.parse()
}
}
}
...
...
}