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

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 : ""

关于arrays - 结果值以'? :'表达式的类型'productsList'和'String'不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49762995/

10-09 08:45