akuritsu
akuritsu

Reputation: 440

Calling a method X amount of times

I am looking for a way to run code X times, using the same piece of code.

Say I wanted to repeat the code:

NSLog(@"repeat");

numberX amount of times:

int numberX;

without having to change any code depending on the value of numberX.

How would this be done? If you need any more explaining, or an explanation of what I am trying to achieve with the code, please comment.

Upvotes: 0

Views: 886

Answers (4)

Otium
Otium

Reputation: 1098

You can use a simple for loop

for(int i = 0; i< numberX; i++) {
// the code you want to repeat

}

Upvotes: 0

user523234
user523234

Reputation: 14834

Assuming that you want to run the tasks synchronously, you can insert your tasks into a Serial Dispatch Queue. It is a FIFO.

Upvotes: 0

MJVDM
MJVDM

Reputation: 3963

user a for loop:

for(int x = 0; i<NUMBER_OF_TIMES; x++){
  NSLog(@"repeat");
}

and make sure you read this Objective-C Primer

Upvotes: 2

brandonbocklund
brandonbocklund

Reputation: 605

Use a for loop! Create your method:

(void) aMethod{
NSLog(@"Repeat");
}

Use method in the original method in a for loop:

int numberX = 123456789;
for(int loopInt = numberX; loopInt >= 0){
aMethod;
}

Upvotes: 0

Related Questions