无法从Google Firestore获取数据,出现线程1:EXC_BAD_INSTRUCTION错误

时间:2018-11-22 20:36:19

标签: swift xcode firebase google-cloud-firestore swift4.2.1

我花了几天的时间来解决这个问题,但我不能。

我收到此错误线程1:当我尝试将Google Firestore中的数据显示到表格视图中时,EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

我在其他视图控制器(链接到另一个集合数据库)中使用相同的代码,效果很好。我不知道发生了什么,它确实在使我耳目一新。

错误出现在代码// Don't use fatalError here in a real app. fatalError("Unable to initialize type \(suppliersProducts.self) with dictionary \(document.data())")的此区域

这是完整的代码:

导入UIKit 导入XLPagerTabStrip 导入Firebase 导入FirebaseFirestore 进口翠鸟

class providerProductsCellOne类:UITableViewCell {

@IBOutlet weak var supplierImage: UIImageView!
@IBOutlet weak var supplierName: UILabel!
@IBOutlet weak var supplierPrice: UILabel!
@IBOutlet weak var supplierDescription: UILabel!

}

class allProductsTableViewController:UITableViewController,IndicatorInfoProvider {

// Variables
var products: [suppliersProducts] = []
var filteredSupplier: [suppliersProducts] = []
var selectedIndex: Int!


override func viewDidLoad() {
    super.viewDidLoad()

    // Hide keyboard
    hideKeyboardOrangutan()

    // Data sources
    tableView.dataSource = self
    tableView.delegate = self

    query = baseQuery()
    print(Firestore.firestore().collection("supplierOneSingle").limit(to: 200))

}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    observeQuery()

}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    stopObserving()
}


fileprivate var query: Query? {
    didSet {
        if let listener = listener {
            listener.remove()
            observeQuery()
        }
    }
}


private var listener: ListenerRegistration?

fileprivate func observeQuery() {
    guard let query = query else { return }
    stopObserving()

    // Display data from Firestore, part one
    listener = query.addSnapshotListener { [unowned self] (snapshot, error) in
        guard let snapshot = snapshot else {
            print("Error fetching snapshot results: \(error!)")
            return
        }
        let models = snapshot.documents.map { (document) -> suppliersProducts in
            if let model = suppliersProducts(dictionary: document.data()) {
                return model
            } else {
                // Don't use fatalError here in a real app.
                fatalError("Unable to initialize type \(suppliersProducts.self) with dictionary \(document.data())")
            }
        }

        self.products = models
        self.tableView.reloadData()
    }
}

fileprivate func stopObserving() {
    listener?.remove()
}


// Firestore database
fileprivate func baseQuery() -> Query {
    return Firestore.firestore().collection("supplierOneSingle").limit(to: 200)
}


// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return products.count
}

// Firestore data
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! supplierProductsCellOne

    let product = products[indexPath.row]
    let url = URL(string: product.productImageSquare)


    cell.supplierName?.text = product.name
    cell.supplierDescription?.text = product.description
    cell.supplierPrice?.text = product.unitPrice

    DispatchQueue.main.async{
        cell.supplierImage?.kf.setImage(with: url)

    }

    return cell


}


// Slider menu indicator
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
    return IndicatorInfo(title: "ALL PRODUCTS")

}

}

这是调试器的说明。

启用新行为时,请审核Date的所有现有用法。在将来的版本中,该行为将更改为新行为,因此,如果不执行这些步骤,则您的应用可能会崩溃。 致命错误:无法初始化类型为provider的产品带字典的[“数量”:1个包装,“产品图片正方形”:https://firebasestorage.googleapis.com/v0/b/honcho-app.appspot.com/o/supplierProducts%2FsquarePhotos%2FProduct-square-5.png?alt=media&token=beff70f8-93e1-44c2-8695-9af27efaccf5,“建议价格”:25000,“类别”:个人护理,“单价”:21000 ,“品牌”:Laurier,“供应商ID”:101,“产品图片矩形”:https://firebasestorage.googleapis.com/v0/b/honcho-app.appspot.com/o/supplierProducts%2FrectanglePhotos%2FProduct-5.png?alt=media&token=520649d0-e76c-45c6-a4eb-c0bdd69eb0f1,“描述”:干燥,干燥的双层空气表面可迅速吸收血液,强大的中心芯将其锁定在内部以防止血液进入泄漏,“大小”:8个填充,“名称”:活动日间填充,“单位SKU”:8992727000048]:文件/Users/eugenio/Documents/yProjects/Honcho/Xcode/merchant-bangkok-bank/merchant/allProductsTableViewController.swift ,第88行 2018-11-23 09:15:31.622656 + 1300 Youtap Merchant [12116:323460]致命错误:无法初始化带有字典[“ Quantity”:1 Pack,“ Product Image Square”:https://firebasestorage.googleapis.com/v0/b/honcho-app.appspot.com/o/supplierProducts%2FsquarePhotos%2FProduct-square-5.png?alt=media&token=beff70f8-93e1-44c2-8695-9af27efaccf5,“建议价格”:25000,“类别”:个人护理,“单价”:21000,“品牌”:Laurier,“供应商ID”:101,“产品图片矩形”:https://firebasestorage.googleapis.com/v0/b/honcho-app.appspot.com/o/supplierProducts%2FrectanglePhotos%2FProduct-5.png?alt=media&token=520649d0-e76c-45c6-a4eb-c0bdd69eb0f1,“描述”:Sofy ,干燥的双层空气层表面迅速吸收血液,强大的中心芯将其锁定在内部以防止泄漏,“大小”:8个打击垫,“名称”:Active Day打击垫,“ Unit SKU”:8992727000048]:file / Users / eugenio /文档/yProjects/Honcho/Xcode/merchant-bangkok-bank/merchant/allProductsTableViewController.swift,第88行 (lldb)

感谢堆帮助

这是SuppliersProducts(词典)的代码

import Foundation
import Firebase

protocol DocumentSerializableSuppliersProducts {
init?(dictionary: [String: Any])
}


struct suppliersProducts {

var brand: String
var category: String
var description: String
var name: String
var productImageRectangle: String
var productImageSquare: String
var quantity: String
var size: String
var suggestedPrice: Int
var supplierID: Int
var unitPrice: Int
var unitSKU: Int


var dictionary: [String: Any] {
    return [

        "Brand": brand,
        "Category": category,
        "Description": description,
        "Name": name,
        "Product Image Rectangle": productImageRectangle,
        "Product Image Square": productImageSquare,
        "Quantity": quantity,
        "Size": size,
        "Suggested Price": suggestedPrice,
        "Supplier ID": supplierID,
        "Unit Price": unitPrice,
        "Unit SKU": unitSKU


    ]


}

}

extension suppliersProducts: DocumentSerializableSuppliersProducts{
    init?(dictionary: [String : Any]) {
        guard let name = dictionary["Name"] as? String,
            let brand = dictionary["Brand"] as? String,
            let category = dictionary["Category"] as? String,
            let productImageRectangle = dictionary["Product Image Rectangle"] as? String,
        let productImageSquare = dictionary["Product Image Square"] as? String,
        let quantity = dictionary["Quantity"] as? String,
        let size = dictionary["Size"] as? String,
        let suggestedPrice = dictionary["Suggested Price"] as? Int,
        let supplierID = dictionary["Supplier ID"] as? Int,
        let unitPrice = dictionary["Unit Price"] as? Int,
        let unitSKU = dictionary["Unit SKU"] as? Int

    else {return nil}
        let description = dictionary["Description"] as? String
        let defaultDescription: String = description ?? "This item has not yet been properly described by our team of writing monkeys"

    self.init(brand: brand, category: category, description: defaultDescription, name: name, productImageRectangle: productImageRectangle, productImageSquare: productImageSquare, quantity: quantity, size: size, suggestedPrice: suggestedPrice, supplierID: supplierID, unitPrice: unitPrice, unitSKU: unitSKU)
}



}

0 个答案:

没有答案
相关问题