我有一个 swift 文件目前用作单例,最终会
是我用来获取所有 API 数据的位置,我有一个用于我的 iboulet 的 View 文件和一个用于更新 tableview 单元格 View 的函数,以及一个包含我的变量和初始化程序的模型文件。一切正常,直到我尝试传递一个数组而不是一个字符串,你能帮我通过我的更新 View 函数传递一个或多个数组吗? Current error
Singleton
class DataServices{
static let instance = DataServices()
private let ackee = [
RecipeInfo(image: "15", title: "Ackee & Saltfish", time: "30 mins", complication: "Easy", serving: "5 people", ingredients: ["Canned Ackee", "Black Pepper", "Salt Fish"], instructions: "again")
]
func getRecipe() -> [RecipeInfo]{
return ackee
}
Update View function for the cell
class RecipeCell: UITableViewCell {
@IBOutlet weak var recipeImage: UIImageView!
@IBOutlet weak var recipeTitle: UILabel!
@IBOutlet weak var recipeTime: UILabel!
@IBOutlet weak var recipeStatus: UILabel!
@IBOutlet weak var mealCount: UILabel!
@IBOutlet weak var ingredients: UILabel!
@IBOutlet weak var instructions: UILabel!
func updateViews(recipe: RecipeInfo){
recipeImage.image = UIImage(named: recipe.image)
recipeTitle.text = recipe.title
recipeTime.text = recipe.time
recipeStatus.text = recipe.complication
mealCount.text = recipe.serving
ingredients.text = recipe.ingredients
instructions.text = recipe.instructions
}
Model with variables and initializer
struct RecipeInfo {
private(set) public var image: String
private(set) public var title: String
private(set) public var time: String
private(set) public var complication: String
private(set) public var serving: String
private(set) public var ingredients: String
private(set) public var instructions: String
init(image: String, title: String, time: String, complication: String, serving: String, ingredients: String, instructions: String) {
self.image = image
self.title = title
self.time = time
self.complication = complication
self.serving = serving
self.ingredients = ingredients
self.instructions = instructions
}
最佳答案
错误很明显:ingredients
被声明为 String
但在 Singleton 中传递了一个数组 ([String]
)。这个名字暗示了一个数组。
private(set)
变量可能看起来很酷,但它们是无稽之谈。这不是 Objective-C。如果你想要常量,将它们声明为常量 (let
)
这就足够了,您可以免费获得初始化程序:
struct RecipeInfo {
let image: String
let title: String
let time: String
let complication: String
let serving: String
let ingredients: [String]
let instructions: String
}
加入数组在 TableView 中显示成分
ingredients.text = recipe.ingredients.joined(separator: ", ")
关于ios - 如何通过我的表格 View 单元格的更新 View 函数传递一个字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951857/