gpt4 book ai didi

ios - 无法将类型 '[String]' 的值转换为预期的参数类型 'String' : while appending arrays to get data from Fireabse in table view cell

转载 作者:可可西里 更新时间:2023-11-01 00:57:26 24 4
gpt4 key购买 nike

在快照中追加数组时出错。我想从快照中的这些数组 EngNamesUrNamescakeImages 中检索所有值,以便这些值可以显示在表格 View 单元格上。但是我可以从 Firebse 检索 Cake Rate LabelEng Name Label 的所有值,但我只想检索所有 cakeRatelabel 来自 firebase 的 rate 值和 EngNamesUrNamescakeImages 我想要的数据的其余部分分别从 Assets 中定义的数组和图像中检索本地。帮助我完成这项工作,我们将不胜感激。

Firebase 结构
enter image description here

View Controller

enter image description here


TestTabelViewController

   import UIKit
import Firebase

class TestTableViewController: UITableViewController {

@IBOutlet var cakeTableView: UITableView!

var ref = FIRDatabase.database().reference()


struct Cake {
let cakeEngNames: String
let cakeUrNames: String
let cakeImages: UIImage
var cakeRates: String
}

var cakes = [Cake]()

var cakeImages = [UIImage(named : "bakery_almond_cake")!,UIImage(named : "bakery_azna_mental_cake")!, UIImage(named : "bakery_black_forest_cake")!, UIImage(named : "bakery_checker_cake")!, UIImage(named : "bakery_cheese_cake")!, UIImage(named : "bakery_chocolate_cake")!, UIImage(named : "bakery_coconut_macaroni_cake")!, UIImage(named : "bakery_cream_cake")!, UIImage(named : "bakery_cream_layer_cake")!, UIImage(named : "bakery_fruit_cake")!, UIImage(named : "bakery_lemon_layer_cake")!, UIImage(named : "bakery_pineapple_cake")!, UIImage(named : "bakery_plain_cake")!,UIImage(named : "bakery_pulm_cake_almond")!,UIImage(named : "bakery_rich_plum_cake")!,UIImage(named : "bakery_swiss_cake")!]

var EngNames = ["Almond Cake", "Azna cake", "Black forest Cake","checker cake", "cheese cake", "chocolate cake","coconut cake", "cream cake", "cream layer cake", "fruit cake", "lemon cake", "pine apple cake", "plain cake", "plum cake", "rich cake", "swiss cake"]

var UrNames = ["Almond Badam cake", "Azna wala cake", "Black kala forest Cake","checker wala cake", "cheese wala cake", "chocolate wala cake","coconut wala cake", "cream wala cake", "cream wala layer cake", "fruit wala cake", "lemon wala cake", "pine apple wala cake", "plain wala cake", "plum wala cake", "rich wala cake", "swiss wala cake"]


override func viewDidLoad() {
super.viewDidLoad()

let almondSnap = ref.child("Hyderabad").child("Bakery").child("Cake")
almondSnap.observe( .value, with: { (snapshot) in
if snapshot.hasChildren(){
for snap in snapshot.children {
if let node = snap as? FIRDataSnapshot , let rate = node.value as? Int {
self.cakes.append(Cake(cakeEngNames: EngNames,
cakeUrNames: UrNames,
cakeImages: cakeImages,
cakeRates: String(rate)))
}
}

self.cakeTableView.reloadData()
}
})

}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell

cell.cakeImage.image = self.cakes[indexPath.row].cakeImages
cell.cakeEngLabs.text = self.cakes[indexPath.row].cakeEngNames
cell.cakeUrLabs.text = self.cakes[indexPath.row].cakeUrNames
cell.cakeRateLabs.text = self.cakes[indexPath.row].cakeRates
return cell
}
}

最佳答案

您正在将数组传递给 Cake 结构初始化程序中的 String 参数。尝试类似的东西

for (i, snap) in snapshot.children.enumerated() {
if let node = snap as? FIRDataSnapshot , let rate = node.value as? Int {
self.cakes.append(Cake(cakeEngNames: EngNames[i],
cakeUrNames: UrNames[i],
cakeImages: cakeImages[i],
cakeRates: String(rate)))
}
}

假设您在顶部定义的三个数组应该映射到不同蛋糕的各种参数。在这里,您从每个数组中获取相关条目,而不是传递整个数组。

关于ios - 无法将类型 '[String]' 的值转换为预期的参数类型 'String' : while appending arrays to get data from Fireabse in table view cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com