Thomas Denney

Enumerate characters of an NSString

The following category method will allow you to iterate over the characters of an NSString with a block:

-(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);
        }
    }
}