我正试图将GoalStruct数组的值附加到另一个也是GoalStruct的数组中,但我还需要一个特定的tableview值。
所以我想实现的是,新数组从“clean”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
最佳答案
您应该迭代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) }