gpt4 book ai didi

ios - 使用自定义数组的 Swift 继承

转载 作者:行者123 更新时间:2023-11-28 08:26:19 25 4
gpt4 key购买 nike

class RecipeBrain: NSObject {
var name: String
var pictureUrl: String
var likes = 0
var ingredients: [Ingredient]
var method: [String]

init(name: String, pictureUrl: String, ingredients: [Ingredient], method: [String]) {
self.name = name
self.pictureUrl = pictureUrl
self.ingredients = ingredients
self.method = method
}
}

class Ingredient{
var name: String
var quantity: Double
var unit: String

init(name: String, quantity: Double, unit: String) {
self.name = name
self.quantity = quantity
self.unit = unit
}
}

class AddRecipe {
var recipeBrain = [RecipeBrain]()
var ingredients = [Ingredient]()
var ingredient = Ingredient(name: "apple", quantity: 1.0, unit: "Kg")
ingredients.append(ingredient)
var recipe1 = RecipeBrain(name: "Recipe1", pictureUrl: "nil", ingrexdients: ingredients, method: ["Method"])
recipeBrain.append(recipe1)
}

我正在尝试在 Swift 中构建食谱应用程序。问题是在我需要字符串、 double 、字符串的地方为其创建成分。

我的想象:一种成分是一系列成分。并创建一个新的食谱,我只是将它附加到 recipeBrain

主要问题:当我尝试将新食谱附加到 recipeBrain 数组时,它说没有声明 recipe1。

(AddRecipe 类的目的只是测试静态数据)

我将其更改为 recipeBrain.append(recipe1) 但我仍然收到错误:预期的声明,当我尝试追加时与配料相同

最佳答案

您正在尝试将食谱和成分对象附加到 AddRecipe 类主体中的数组中,但您做不到。

如果你在方法体中这样做,一切都会好起来的,例如在 init() 方法中:

class AddRecipe {
var recipeBrain = [RecipeBrain]()
var ingredients = [Ingredient]()

init()
{
var ingredient = Ingredient(name: "apple", quantity: 1.0, unit: "Kg")
ingredients.append(ingredient)
var recipe1 = RecipeBrain(name: "Recipe1", pictureUrl: "nil", ingredients: ingredients, method: ["Method"])
recipeBrain.append(recipe1)
}

关于ios - 使用自定义数组的 Swift 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882178/

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