I need to search each object in my array "heroes" for each hero(abathur, chromie, etc...) who has an item in it's synergy array equal to "Xul".("Xul" will eventually get changed to a variable input by the user). Then I need to increase the "pickValue" property for each of those that match.
So in this example the search would match chromie and increase its pick value.
this filter below lets me put the matched items into another array but i don't think filter will work for what i want because i need to keep the array "heroes" in tact and just increase the property "pickValue"
let filteredHeroes = heroes.filter( { $0.synergy.contains("Xul") } )
I've driven myself crazy looking for how to do this and I can't find it. The tutorial i've been following doesn't touch on this either. Below is the other relevant info.
class Hero{
var name: String
var role: String
var synergy: [String]
var strong: [String]
var weak: [String]
var pickValue = 0
init(name: String, role: String, synergy: [String], strong: [String], weak: [String]){
self.name = name
self.role = role
self.synergy = synergy
self.strong = strong
self.weak = weak
}
}
// there are about 50 of these "heroes"
var abathur = Hero(name: "Abathur", role: "Ranged Specialist", synergy: ["Illidan", "Chromie"], strong: ["Tracer", "Murky"], weak: ["Zeratul", "Nova"])
var chromie = Hero(name: "Chromie", role: "Ranged Assassin", synergy: ["Xul", "Johanna"], strong: ["Abathur", "Zagara"], weak: ["Tracer", "Greymane"])
var heroes = [Hero]()
heroes.append(abathur) // appended all heroes
heroes.append(chromie)
filterdoes not modify the original collection, it simply creates a new array with the matched items.