swift - searching an array inside of an array of object -


i need search each object in array "heroes" each hero(abathur, chromie, etc...) has item in it's synergy array equal "xul".("xul" changed variable input user). need increase "pickvalue" property each of match.

so in example search match chromie , increase pick value.

this filter below lets me put matched items array don't think filter work want because need keep array "heroes" in tact , increase property "pickvalue"

let filteredheroes = heroes.filter( { $0.synergy.contains("xul") } ) 

i've driven myself crazy looking how , can't find it. tutorial i've been following doesn't touch on either. below 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 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 heroes heroes.append(chromie) 

all answers provided right , allow modify pickvalue in original array without losing element.

the reason heroes:[hero] array of objects not array of structs. when manipulate array of objects .map(), .filter() , likes, new array obtain contains references same object instances original array. making changes new arrays modifies hero in original array well.

if hero had been struct instead indeed have had problem seem worried about.


Comments