I have an object called brain
brain has 8 properties that contain an “Animal” object.
brain.animal1,
brain.animal2,
animal3,
…
brain.animal8
I then have three other properties called
brain.selectedAnimal1, brain.selectedAnimal2, brain.selectedAnimal3
What I would like to do is loop through all of the 8 animal properties… and if one of those animal properties matches up with selectedAnimal1 property, i want to set the animal property to nil.
I only want to set one animal property to nil (i.e. if two of the animal properties match the selectedAnimal1 property I only want the first one to be set to nil, not both)
Then I want to do the same loop but compare with selectedAnimal2 and selectedAnimal3 properties and set animal properties to nil if they match with a selectedAnimal.
Can anyone help with this? I’m just not quite good enough yet with objective-c to get this to work without writing about 50 lines of code
So to recap… I start with 8 animal properties and would like to end with 5 animal properties (3 being set to nil)
Thanks in advance,
It seems very non-flexible and non-extendible to have animal1, animal2, … and selectedAnimal1, selectedAnimal2, …
You should really look into using collections (NSMutableArray or NSMutableSet), say animals and selectedAnimals, then you can loop through you animal objects using fast enumeration:
If you find an animal which is in both you can remove it from animals. Careful not to change a mutable array while enumerating it, you’ll get crashes. I think you should use NSMutableSet (which have union, minus operators and so on, making it easy to subtract common elements from one set).