0

I want to execute my For loop faster but i dont have idea about Fast Enumeration. Can any one suggest me how to change this For loop in fast enumeration.

NSString *strCorrectWord;
    for(i=0;i<[self.strCorrectWord length];i++)
    {


    }
jrturton
  • 118,105
  • 32
  • 252
  • 268

3 Answers3

0

--> Fast enumeration is done over collection in objective c for example if there is a NSArray of strings then for each string we implement fast enumeration of For loop as:-

for(NSString *str in NSArray object)
{
    statement;
}
Ravi Sharma
  • 975
  • 11
  • 29
0

NSString does not implement NSFastEnumeration, meaning that there is not really an alternative syntax to make your loop faster (except for those mentioned here, perhaps).

The performance bottleneck is highly likely to be the code you are executing inside each iteration of the loop rather than the mechanics of the loop itself, but you haven't included that in the question.

Fast enumeration is simply a replacement for the slower methods that used to be used to enumerate through collections.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
0

If you want to loop through each char of a given string

NSString *strCorrectWord = @"Test";

NSRange range = NSMakeRange(0, strCorrectWord.length);

[strCorrectWord enumerateSubstringsInRange:range options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

    //Substring is each char
    NSLog(@"%@", substring);
}];

enumerate with a block is a faster solution most of the time. Using the options you can change it to give you a substring every char or every word.

Ste Prescott
  • 1,789
  • 2
  • 22
  • 43