Skip to content

Instantly share code, notes, and snippets.

@programmingthomas
Created October 6, 2013 16:49
Show Gist options
  • Save programmingthomas/6856295 to your computer and use it in GitHub Desktop.
Save programmingthomas/6856295 to your computer and use it in GitHub Desktop.
NSString character enumeration category
-(void)enumerateCharacters:(EnumerationBlock)enumerationBlock
{
const unichar * chars = CFStringGetCharactersPtr((__bridge CFStringRef)self);
//Function will return NULL if internal storage of string doesn't allow for easy iteration
if (chars != NULL)
{
NSUInteger index = 0;
while (*chars) {
enumerationBlock(*chars, index);
chars++;
index++;
}
}
else
{
//Use IMP/SEL if the other enumeration is unavailable
SEL sel = @selector(characterAtIndex:);
unichar (*charAtIndex)(id, SEL, NSUInteger) = (typeof(charAtIndex)) [self methodForSelector:sel];
for (NSUInteger i = 0; i < self.length; i++)
{
const unichar c = charAtIndex(self, sel, i);
enumerationBlock(c, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment