ios - 尝试插入按钮时出现问题? ( swift 用户界面)

标签 ios swift sqlite swiftui

我正在从 UIKit 过渡到 SwiftUI,但不确定为什么我在 VStack 声明行处收到错误“静态成员‘前导’不能在‘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()
   }
}

最佳答案

编辑 2020 年 4 月 10 日: 问题是你的 VStack 有超过 10 个 subview ,而它最多只接受 10 个。要解决这个问题,请将一些 subview 放在 Group 中,其中包含没有视觉效果,但允许 VStack 工作:

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

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

在 Swift 的 future 版本中,编译器正在得到改进,以便更容易诊断此类错误,但现在这是这个问题的答案。

为后代保留原始答案:

<小时/>

当真正的问题完全独立时,我遇到过很多这样的错误。在这种情况下,我建议将您的 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 中的一个错误。希望这对您有所帮助!

关于ios - 尝试插入按钮时出现问题? ( swift 用户界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963937/

相关文章:

ios - 在 Swift 2 中对字典数组进行排序

ios - 在标签上打印 Instagram 帖子回复时出现问题

swift - 如何使 RealmSwift RealmOptional 与 Swift Codable 兼容?

python - 创建表时将列名作为变量

ios - 如何在 Swift 中向 Collection View Cell 添加删除按钮?

ios - 在 iOS 上测试蓝牙

ios - 如何从图像路径中获取文件名

ios - 在 iOS 8 中请求本地通知权限,但仍然有应用程序支持 iOS 7

.net - sqlite3 的 System.DllNotFoundException

c++ - sqlite 和可能的空值