I have a complex array made up as the following shows:
foodArray = [NSMutableArray arrayWithObjects:
[Food cat:@"cereals" risk:@"low" name:@"Frosties" image:[UIImage imageNamed:@"frosties.jpg"]],
[Food cat:@"cereals" risk:@"low" name:@"Coco Pops" image:[UIImage imageNamed:@"cocopops.jpg"]],
[Food cat:@"cereals" risk:@"low" name:@"Bran Flakes" image:[UIImage imageNamed:@"branflakes.jpg"]],
[Food cat:@"cereals" risk:@"low" name:@"Golden Crisp" image:[UIImage imageNamed:@"goldencrisp.jpg"]],
[Food cat:@"cereals" risk:@"low" name:@"Honey Smacks" image:[UIImage imageNamed:@"honeysmacks.jpg"]],
[Food cat:@"cereals" risk:@"low" name:@"Lucky Charms" image:[UIImage imageNamed:@"luckycharms.jpg"]], nil];
Now what I'm trying to do is randomise all the items by 'name' and then I want to filter where the cat = cereals and the risk = low, and only choose the first 3 foods. (more items will be added later with different cat and risk values).
I've been randomising using the following:
for (int i = 0; i<[foodArray count]-1; i++)
{
NSUInteger randomIndex = arc4random() % [foodArray count];
[foodArray exchangeObjectAtIndex:i withObjectAtIndex:randomIndex];
}
But this has meant that my foodArray
, which was initially and NSArray
has been changed to an NSMutableArray
. Now how can I go about filtering the array? I'm a bit stuck on this and how to limit to 3 and search two parts of the array.
EDIT
I am trying to return the name of the 3 chosen foods and am using the below code:
NSArray *threeFoods=[self getFoodsFromArray:foodArray withRisk:@"low" inCategory:alternativeFood count:3];
NSString *testText = @"";
for (NSString *test in threeFoods) {
testText = [testText stringByAppendingFormat:@"%@\n", test];
}
altFood.text = testText;
This works to a degree to populate my textview. However, it returns data in the form '