È possibile accelerare -characterAtIndex:
convertendolo in sua forma IMP prima:
NSString *str = @"This is a test";
NSUInteger len = [str length]; // only calling [str length] once speeds up the process as well
SEL sel = @selector(characterAtIndex:);
// using typeof to save my fingers from typing more
unichar (*charAtIdx)(id, SEL, NSUInteger) = (typeof(charAtIdx)) [str methodForSelector:sel];
for (int i = 0; i < len; i++) {
unichar c = charAtIdx(str, sel, i);
// do something with C
NSLog(@"%C", c);
}
EDIT: Sembra che il CFString
Reference contiene il seguente metodo:
const UniChar *CFStringGetCharactersPtr(CFStringRef theString);
Questo significa che è possibile effettuare le seguente:
const unichar *chars = CFStringGetCharactersPtr((__bridge CFStringRef) theString);
while (*chars)
{
// do something with *chars
chars++;
}
Se non si desidera allocare m Emory per far fronte al buffer, questa è la strada da percorrere.
fonte
2012-04-17 21:05:41
Se sei così preoccupato per le prestazioni, sarebbe meglio utilizzare NSData e accedere alla matrice di byte di quello. – joerick
Si scopre che CFString in realtà ha un modo per farlo, in CFStringGetCharactersPtr ... –
"... ma che sarà più lento di ..." - questo si chiama ** ottimizzazione prematura **. Stai facendo delle ipotesi sulle prestazioni ancor prima di sapere se le prestazioni saranno un problema. Dovresti implementarlo in modo ovvio (usando 'characterAtIndex') e ottimizzarlo solo se hai problemi di prestazioni. – Sulthan