添加两个结构数组

时间:2019-04-29 15:08:32

标签: swift

我正在尝试将我的GoalStruct数组的值附加到另一个也是GoalStruct的数组,但是对于特定的表格视图,我需要再加上一个值。

所以我要实现的是,新数组从“干净的” GoalStruct数组中获取所有值以及一个附加值。

struct GoalStruct:Codable {
var title: String
var day: Int
var month: Int
var year: Int
var date: String
}

struct SelectedGoalStruct:Codable {
var GoalStruct: GoalStruct
var isSelected: Bool
}

这是我尝试过的方法,但由于其整个数组而无法工作:

global.selectedBacklogGoals = SelectedGoalStruct(GoalStruct: global.goalsB, isSelected: false)
//goalsB is a array with all values in it while SelectedBacklogGoals is empty

1 个答案:

答案 0 :(得分:2)

您应该迭代global.goalsB数组并创建SelectedGoalStruct实例

for goal in global.goalsB {
    selectedBacklogGoals.append(SelectedGoalStruct(GoalStruct: goal, isSelected: false))
}

global.selectedBacklogGoals = global.goalsB.map { SelectedGoalStruct(GoalStruct: $0, isSelected: false) }