【问题标题】:Things go wrong when trying to insert button? (SwiftUI)尝试插入按钮时出现问题? (斯威夫特用户界面)
【发布时间】:2020-04-11 02:45:46
【问题描述】:

我正在从 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()
   }
}

【问题讨论】:

  • 我也经常遇到这个问题。这基本上意味着你有一个编译错误 - 在其他地方 - 但 SwiftUI 编译器正在搞砸它并给你一个错误的错误。我想出如何找到它的唯一方法是注释掉部分代码,直到错误消失或显示真正的错误。

标签: ios swift sqlite swiftui


【解决方案1】:

编辑 4/10/20: 问题是您的VStack 有超过 10 个子视图,并且它最多只能接受 10 个。要解决此问题,请将一些子视图粘贴到 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 的未来版本中,编译器正在得到改进,以便更容易诊断出此类错误,但现在这里是这个问题的答案。

原始答案保留在下面以供后代使用:


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

【讨论】:

  • 我尝试实施您的建议,但不幸的是我仍然遇到同样的错误。
  • @ganele892 如果你还在寻找答案,我已经解决了
猜你喜欢
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
相关资源
最近更新 更多