Brandon A
Brandon A

Reputation: 8279

Objective-C: Count to every "tenth" in array

I was wondering how do I get sort through an do something different based on every "tenth" item in an array. I don't know much but I think it would kind of go like this...

for (NSDictionary *object in array) {

    if (0==(object % 10)) {

        //DO SOMETHING
    }

}

But this is obviously pseudo code that doesn't work. Can any one help me out with this?

Upvotes: 1

Views: 895

Answers (4)

valeriana
valeriana

Reputation: 159

Objective-C help:

It took me a little while, I wasn't able to make the above examples work, but this worked for me... might be able to help someone if you need to add just every 10th object to the array:

startTime3 = [[NSMutableArray alloc]init];
     int j;
     int i;
     for ( j = 0, i = 0; i < 12; i++, j += 10) {
         [startTime3 addObject:[NSString stringWithFormat:@"%i", j]];
         NSLog(@"i = %i, j = %i", i, j);

     } 

Output of startTime3 = [0,10,20,30,40,50,60,70,80,90,100,110]

In my project startTime3 = [0,5,10,15,20,25,30,35,40,45,50,55] using j += 5 works for my minutes timer array.

(when I was only using 'j', like the examples above, I was only getting back 2 objects? If anyone runs into this issue, this solved it for me. )

Upvotes: 0

Ryan
Ryan

Reputation: 4884

Try this.

for (NSInteger i = 0 ; i < array.count ; i++)
{
    if(i%10 == 0)
    {
        // Do Something
    }
}

or

for (NSDictionary *object in array)
{
    NSInteger index = [array indexOfObject:object];
    if(index%10 == 0)
    {
        // Do Something
    }
}

Added

Assume that the array is NSArray.

NSArray *array;
NSMutableArray *tempArray = [NSMutableArray array];
for (NSInteger i = 0 ; i < array.count ; i++)
{
    if(i%10 == 0)
    {
        [tempArray addObject:/*some object*/];
    }
    [tempArray addObject:[array objectAtIndex:i]];
}
array = [NSArray arrayWithArray:tempArray];

if array was like below and add A at every 10th.

|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|....
// 9 is the first 10th
// 19 is the second 10th

will be

|0|1|2|3|4|5|6|7|8|a|9|10|11|12|13|14|15|16|17||18|a|19....

Added

as rmaddy commented, to insert object, don't need to iterate all objects.

for (NSInteger i = 0 ; i < array.count ; i+=10)

You can use this. However you can just insert objects at index.

NSMutableArray *array;  // this is the original array;
NSArray *objects;       // this is the objects to insert array;
for(NSInteger i = 0 ; i < objects.count ; i++)
{
    [array insertObject:objects[i] atIndex:9+(i*11)];
}

Upvotes: 4

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

-enumerateObjectsUsingBlock: gives you both index and object reference.

[array enumerateObjectsUsingBlock:^(NSDictionary *object, NSUInteger i, BOOL *stop) {
    if (0 == (i % 10)) {
        //DO SOMETHING
    }
}];

Upvotes: 0

prototypical
prototypical

Reputation: 6751

How about :

for (int index = 0;index < array.count;index += 10)
{
     NSDictionary *object = array[index];
     // do whatever with object;
}

Upvotes: 4

Related Questions