【问题标题】:Swift : retrieving struct values in swiftui view is not workingSwift:在 swiftui 视图中检索结构值不起作用
【发布时间】:2020-08-05 17:25:08
【问题描述】:

我是 swift 新手,需要定义某种形式的全局字典,以便我可以在整个项目中访问内容。我的理解是结构类可以用于此

我创建了一个结构并为其附加了值,现在我想访问视图中的每个值

这是我的产品结构

struct Product {
    
    let name: String
    let aisleNo:Int
    let location_section: Int
    let location_zone: String
    let productPrice: Int
}

然后创建一个全局

import Foundation

struct Global {
    static var productList = [Product]() 
}

这就是我将许多产品附加到产品的方式

class SearchResult : ObservableObject {
        var productList = [Product]()
         //There could be hundreds of product in the array
        for product in productArray {
            let productName = product.productName!
            let aisleNo = product.productLocation_aisle.value!
            let location_section = product.productLocation_section.value!
            let location_zone = product.productLocation_zone!
            let productPrice =  product.productPrice.value!
            let product_real_id = product._id!
            
            Global.productList.append(Product(name: productName, aisleNo: aisleNo, location_section: location_section, location_zone: location_zone, productPrice: Int(productPrice)))

}

这是我想要显示产品内容的搜索结果视图

struct SearchResultView: View {
      var searchResults = Global.productList

        var body: some View {

               VStack {
               List {
                ForEach(model.searchResults, id: \.self) { text in
                    Text(text)
                }
               
               
               }

           }
              }
 }

我似乎可以让它显示在 searchResultView 中。做错了什么? 我不断收到此错误

通用结构 'ForEach' 要求 'Product' 符合 'Hashable' Initializer 'init(_:)' 要求 'Product' 符合 'StringProtocol'

【问题讨论】:

  • 请在我的编辑中查看并实施最新更改
  • @JulianSilvestri 谢谢。它可以解决可哈希错误。但是你知道如何修复第二个错误初始化程序'init(_:)' 要求 'Product' 符合 'StringProtocol'
  • 字符串协议错误没有随着哈希值消失吗?
  • @JulianSilvestri 不,它没有

标签: swift swiftui


【解决方案1】:

您需要将“searchResults”设置为等于您的“productList” 现在你的 searchResults 是空的。它只是作为结构的一个实例存在,其中没有数据。

一种选择是将变量范围设为全局,然后将新变量 = 设置为它

self.searchResults = Global.productList 

--编辑

你很接近。 您在此处设置 var 的位置

var searchResults = Global.productList

应该是这样的。

var searchResults = [Product]() // ->Creates an instance of the struct object

然后将其设置为等于您的全局数组。

self.searchResults = Global.productList

您还应该删除多余的变量var productList = [Product]()

还有一些注意事项

for product in productArray {
      let productName = product.productName!
      let aisleNo = product.productLocation_aisle.value!
      let location_section = product.productLocation_section.value!
      let location_zone = product.productLocation_zone!
      let productPrice =  product.productPrice.value!
      let product_real_id = product._id!
            
      Global.productList.append(Product(name: productName, aisleNo: aisleNo, location_section: location_section, location_zone: location_zone, productPrice: Int(productPrice)))
}

你正在使用所有的 let 变量做额外的工作。 更好的方法是这样做。

for product in productArray {

      Global.productList.append(Product(name: product.name, aisleNo: product.aisleNo, location_section: product.location_section, location_zone: product.location_zone, productPrice: Int(product.productPrice)))
}

编辑 - 可散列错误

试试这个

struct Product: Hashable {
    let name: String
    let aisleNo:Int
    let location_section: Int
    let location_zone: String
    let productPrice: Int
}

【讨论】:

  • 如何在 View 中实现
  • @learner101 您需要将变量设置为 GLOBAL 以便您可以保持结构不变。这是通过创建一个新的 swift 文件,创建一个名为“Global”的结构并添加 -> static var productList = [Product]() 来完成的。那么你需要更改你的变量名,你将它附加到 -> Global.productList,这允许你在任何地方访问这个变量,然后在你需要数据的视图中,设置你的新变量 self.searchResults = Global.productLIst跨度>
  • 我仍然无法在视图中显示 productList。请查看我更新的问题
  • 好的,视图显示有关使产品符合“可哈希”的错误。我更新了问题以显示视图和错误消息
猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2022-12-12
  • 2020-12-28
  • 1970-01-01
  • 2020-11-05
  • 2020-06-20
  • 1970-01-01
相关资源
最近更新 更多