我有一个名为 FoodItem 的结构,它有 3 个属性
struct FoodItem {
foodName: String
foodAmount: Double
amountMetric: String
}
当用户向应用程序添加食物时,他/她分 2 个步骤完成。第一步只需要这 3 件事。但是,第 2 步需要一些额外的东西......喜欢 金额步骤: 整数, 步骤标题: 字符串...
protocol CookingInstructions {
var amountOfSteps: Int { get }
var stepTitle: String { get }
}
是否可以将步骤 2 部分添加为协议,但在步骤 1 完成后添加协议? 所以在步骤 2 之前,结构体不知道协议,但随后添加它?
最后,我只希望步骤 1 只需要 3 件事,然后第 2 步需要不同的几件事。
不,不可能在运行时更改结构的一致性。
也许您可以为步骤 2 添加另一个结构,然后添加一个包含您的 FoodItem 和烹饪说明的结构?
喜欢这个:
struct FoodItem {
foodName: String
foodAmount: Double
amountMetric: String
}
struct CookingInstructions {
var amountOfSteps: Int
var stepTitle: String
}
struct Composite {
var step1: FoodItem
var step2: CookingInstructions?
}