0

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 '

Prateek
  • 1,724
  • 3
  • 15
  • 27
  • possible duplicate of [canonical way to randomize an NSArray in Objective C](http://stackoverflow.com/questions/791232/canonical-way-to-randomize-an-nsarray-in-objective-c) – Sam B Jul 19 '14 at 10:43
  • What do you mean by *"randomise by 'name'"*? Why don't you filter the array first, shuffle the filtered array and then pick the first 3 entries? – Martin R Jul 19 '14 at 10:47
  • That's probably a better way to do it haha! How can I do that? As I'm filtering both 'cat' and 'risk' – Prateek Jul 19 '14 at 11:12

1 Answers1

2

You can use this method to filter the array and then return n random entries from the array -

- (NSArray *)getFoodsFromArray:(NSArray*)foodArray withRisk:(NSString *)risk inCategory:(NSString *)cat count:(int)count
{
    NSMutableArray *filteredArray=[[foodArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(risk==%@)and (cat==%@)",risk,cat]] mutableCopy];

    if (count < filteredArray.count) {

        for (int i=0;i<count;i++)
        {
            int randomIndex=arc4random_uniform((int)(filteredArray.count-i));
            [filteredArray exchangeObjectAtIndex:randomIndex withObjectAtIndex:filteredArray.count-i-1];

        }
        [filteredArray removeObjectsInRange:NSMakeRange(0, filteredArray.count-count)];
    }
    return filteredArray;
}

You invoke the method as follows -

NSArray *threeFoods=[self getFoodsFromArray:foodArray withRisk:@"low" inCategory:@"cereals" count:3];

You can access the three food names as follows -

for (Food *food in threeFoods) {
    testText = [testText stringByAppendingFormat:@"%@\n", food.name];
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • This is brilliant thank you! Just you pick your brains a little more. From this array, I then want to return the names of the 3 random foods in a textview. If you could look at the original post. I have edited it to show you my current problem with returning the values. Thanks! – Prateek Jul 19 '14 at 12:00
  • You, sir, are a lifesaver! I was nearly there! Thanks so much! – Prateek Jul 19 '14 at 12:06