尝试插入按钮时出现问题? (SwiftUI)

时间:2019-11-20 21:50:36

标签: ios swift sqlite swiftui

我正在从UIKit过渡到SwiftUI,并且不确定为什么在VStack声明行出现错误,“静态成员'leading'不能用于'Horizo​​ntalAlignment'类型的实例”。仅当我插入按钮时才会发生这种情况,将代码的按钮行注释掉时没有错误。

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()
   }
}

1 个答案:

答案 0 :(得分:0)

当真正的问题完全分开时,我有很多该错误的实例。在这种情况下,我建议将您的Button代码更改为此:

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.")
    }
}) {
    Text("Search for customer")
}

(将标签移到尾随的闭包中,因为这对我来说似乎更好)。这使您的代码如下:

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.")
            }
        }) {
            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中的错误。希望这会有所帮助!