Skip to content

Instantly share code, notes, and snippets.

@daehn
Created May 10, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daehn/c8c84c357739b9466ad1 to your computer and use it in GitHub Desktop.
Save daehn/c8c84c357739b9466ad1 to your computer and use it in GitHub Desktop.
Iterate over all characters of a NSString as NSStrings.
typedef void(^StringEnumerationBlock)(NSString *characterString, NSUInteger index, BOOL *stop);
- (void)enumerateCharactersUsingBlock:(StringEnumerationBlock)block {
NSParameterAssert(block);
BOOL stop = NO;
for (NSUInteger i = 0; i < self.length; i++) {
const unichar character = [self characterAtIndex:i];
NSString *characterString = [NSString stringWithCharacters:&character length:1];
block(characterString, i, &stop);
if (stop) {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment