Created
October 6, 2013 16:49
-
-
Save programmingthomas/6856295 to your computer and use it in GitHub Desktop.
NSString character enumeration category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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