我正在尝试将数据从一个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
}
答案 0 :(得分:3)
在这一行:
let updaterId = postData.count > index ? postData[index] : ""
postData
被声明为productsList
的数组,因此postData[index]
是productsList
。但""
是一个字符串。它们必须是相同的类型 - 要么两者都必须是字符串,要么两者都必须是productsList
个实例。
你可能意味着它们都是字符串;你可能意味着
let updaterId = postData.count > index ? postData[index].id : ""
答案 1 :(得分:2)
由于您似乎想要获取用户ID,请更改:
let updaterId = postData.count > index ? postData[index] : ""
为:
let updaterId = postData.count > index ? postData[index].id : ""
这会在String
的两侧为您提供:
。