【发布时间】:2020-04-11 02:45:46
【问题描述】:
我正在从 UIKit 过渡到 SwiftUI,但不知道为什么在 VStack 声明行出现错误,“静态成员 'leading' 不能用于 'HorizontalAlignment' 类型的实例”。只有在我插入按钮时才会出现这种情况,当我将代码的按钮行注释掉时不会出现错误。
import SwiftUI
import SQLite3
struct SearchCustomers: View {
@State private var first_name: String = ""
@State private var last_name: String = ""
@State private var customerNumber: Int = 0
var customers: [Customer] = []
//var db: SQLiteDatabase
// File path -> /Users/macbookpro/Downloads/classicmodels.sqlite
// dummy data to remove later
let customer1 = Customer(customerNumber: 12, customerName: "A", contactLastName: "A", contactFirstName: "A", phone: "A", addressLine1: "A", addressLine2: "A", city: "A", state: "A", postalCode: "A", country: "A", salesRepEmployeeNumber: 12, creditLimit: 12.35)
// Dictionary to store search results from sqlite
var search_results: [String: String] = [:]
// var customerDB = SQLiteDatabase.open(path: "")
var body: some View {
VStack(alignment: .leading) {
Text("Search by name").font(Font.title.weight(.regular))
HStack {
Text("First Name")
TextField("Enter text", text: $first_name)
Text("Last Name")
TextField("Enter text", text: $last_name)
}
Button(action: {
// open connection to the db
do {
let db = try SQLiteDatabase.open(path: "/Users/macbookpro/Downloadsclassicmodels.sqlite")
print("opened connection")
} catch {
print("Unable to open db connection.")
}
}, label: {
Text("Search for customer")
})
Spacer()
Spacer()
Text("Search by customer number").font(Font.title.weight(.regular))
HStack {
Text("Customer Number: ")
TextField("Enter text", text: $first_name)
}
Spacer()
Spacer()
Spacer()
VStack(alignment: .leading) {
Text("Query Results").font(Font.title.weight(.light))
List {
ForEach(customers, id: \.customerNumber) { customer in
Text("customer")
}
}
}
}
.navigationBarTitle("Search for Customers")
}
}
struct SearchCustomers_Previews: PreviewProvider {
static var previews: some View {
SearchCustomers()
}
}
【问题讨论】:
-
我也经常遇到这个问题。这基本上意味着你有一个编译错误 - 在其他地方 - 但 SwiftUI 编译器正在搞砸它并给你一个错误的错误。我想出如何找到它的唯一方法是注释掉部分代码,直到错误消失或显示真正的错误。