我有两个结构“餐”和“食物” 我想创建一个数组
所以这些是我的结构
struct Meal
{
var name : String;
var food : [Food];
}
struct Food
{
var name :String;
var description : String;
}
这是我正在编写的用于创建数组的代码:
var meals :[Meal] = [
Meal(name:"breakfast",food : [(name:"pancakes",description:"bk1"),(name:"waffles",description:"bk2")]),
Meal(name:"lunch",food : [(name:"pasta",description:"lunch1"),(name:"pizza",description:"lunch2")]),
Meal(name:"dinner",food : [(name:"rice",description:"din1"),(name:"noodles",description:"din2")]),
];
但是它给出了一个错误:“无法将类型'((名称:字符串,描述:字符串)'的值转换为预期的元素类型'食物'”。
我该如何解决?
答案 0 :(得分:1)
考虑用于创建Food
实例的语法。考虑一下您将如何正常地创建一个。
let someFood = Food(name: "pancakes", description: "bk1")
在数组中使用相同的语法。
Meal(name: "breakfast", food: [Food(name: "pancakes", description: "bk1"), Food(name: "waffles", description: "bk2")]),