Roman H
Roman H

Reputation: 251

Add two struct-arrays

I am trying to append the value of my GoalStruct array to another array which is also a GoalStruct but with one more value I need for a specific tableview.

So what I want to achieve is that, the new array gets all the values from the "clean" GoalStruct array with one additional value.

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
}

this is what I have tried but doesn't work because its a whole array:

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

Upvotes: 0

Views: 46

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15778

You should iterate the global.goalsB array and create SelectedGoalStruct instances

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

Or

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

Upvotes: 2

Related Questions