Arrays ';中的结果值:';表达式的类型不匹配';产品列表';和';字符串';

Arrays ';中的结果值:';表达式的类型不匹配';产品列表';和';字符串';,arrays,json,swift,segue,Arrays,Json,Swift,Segue,我试图将数据从一个VC发送到下一个VC,但我在上一个函数的标题中声明了一个错误,我在该函数中声明了UpdateId常量。考虑到这有助于我解析JSON,我更希望它们都是productsList类型。我知道那行代码中有引号,但我只是用一个字符串作为例子 import UIKit import Firebase import FirebaseAuth import FirebaseDatabase import FirebaseStorage class ListViewController: UI

我试图将数据从一个VC发送到下一个VC,但我在上一个函数的标题中声明了一个错误,我在该函数中声明了UpdateId常量。考虑到这有助于我解析JSON,我更希望它们都是productsList类型。我知道那行代码中有引号,但我只是用一个字符串作为例子

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage

class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableViewProducts: UITableView!
    var delegate: ListViewController?

    var ref:DatabaseReference?
    var databaseHandle: DatabaseHandle?

    var postData = [productsList]()

    override func viewDidLoad() {
        super.viewDidLoad()
        ref = Database.database().reference().child("AudioDB")
        loadProducts()
    }

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

    func loadProducts() {
        ref?.observe(DataEventType.value, with: { (snapshot) in
            var newSweets = [productsList]()

            for post in snapshot.children {
                let postObject = productsList(snapshot: post as! DataSnapshot)
                newSweets.append(postObject)
                print(self.postData)

            }
            self.postData = newSweets
            self.tableViewProducts.reloadData()
        }) { (error:Error) in
            print(error)
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        performSegue(withIdentifier: "showDetails", sender: self)
    }

    //This places the text on the ViewControllerTableViewCell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

        let sweet = postData[indexPath.row]

        cell.idLbl.text = sweet.id
        cell.nameLbl.text = sweet.p_name

        if let profileImageUrl = sweet.image {
            let url = URL(string: profileImageUrl)
            URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
                if error != nil {
                    print(error)
                    return
                }
                DispatchQueue.main.async {
                cell.productImage.image = UIImage(data: data!)
                }
            }).resume()
        }
        return cell
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? ProductViewController,
            let index = tableViewProducts.indexPathForSelectedRow?.row {

            // Check whether postData array count is greater than index
            let updaterId = postData.count > index ? postData[index] : ""

            // Initialize "productsList" instance and assign the id value and send this object to next view controller
            let updater = productsList(id: updaterId, p_name: String)
            updater.id = updaterId
            destination.updater = updater
        }
    }
}
以下是产品清单:

struct productsList {

    let id: String!
    let p_name: String!
    let image: String!


    init(id: String, p_name: String) {
        self.id = id
        self.p_name = p_name
        self.image = p_name

    }

    init (snapshot:DataSnapshot) {
        var dict = snapshot.value as! [String: AnyObject]
        id = dict["id"] as! String
        p_name = dict["p_name"] as! String
        image = dict["image"] as! String

    }
在这方面:

let updaterId = postData.count > index ? postData[index] : ""
postData
被声明为
productsList
的数组,因此
postData[index]
productsList
。但是
是一个字符串。它们必须是相同的类型-或者两者都必须是字符串,或者两者都必须是
productsList
实例

你可能想让它们都成为弦;你可能是说

let updaterId = postData.count > index ? postData[index].id : ""
在这方面:

let updaterId = postData.count > index ? postData[index] : ""
postData
被声明为
productsList
的数组,因此
postData[index]
productsList
。但是
是一个字符串。它们必须是相同的类型-或者两者都必须是字符串,或者两者都必须是
productsList
实例

你可能想让它们都成为弦;你可能是说

let updaterId = postData.count > index ? postData[index].id : ""

由于您似乎想要获取用户id,请更改:

let updaterId = postData.count > index ? postData[index] : ""
致:


这将在

两侧为您提供一个
字符串,因为您似乎想要获取用户id,请更改:

let updaterId = postData.count > index ? postData[index] : ""
致:


这将在
的两侧为您提供一个
字符串

什么是
产品列表
?FYI-Class/struct/enum名称应以大写字母开头。这是帮助我解析JSON的my struct。什么是
产品列表
?FYI-Class/struct/enum名称应以大写字母开头。这是帮助我的my struct解析我的json