jjx*_*tra 5 string cocoa-touch objective-c ios
如何通过拉出每个unichar来枚举NSString?我可以使用characterAtIndex,但这比通过递增的unichar*更慢.我没有在Apple的文档中看到任何不需要将字符串复制到第二个缓冲区的内容.
这样的事情是理想的:
for (unichar c in string) { ... }
Run Code Online (Sandbox Code Playgroud)
要么
unichar* ptr = (unichar*)string;
Run Code Online (Sandbox Code Playgroud)
Ric*_*III 11
您可以-characterAtIndex:
通过首先将其转换为IMP表单来加快速度:
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:似乎CFString
参考包含以下方法:
const UniChar *CFStringGetCharactersPtr(CFStringRef theString);
Run Code Online (Sandbox Code Playgroud)
这意味着您可以执行以下操作:
const unichar *chars = CFStringGetCharactersPtr((__bridge CFStringRef) theString);
while (*chars)
{
// do something with *chars
chars++;
}
Run Code Online (Sandbox Code Playgroud)
如果您不想分配内存来处理缓冲区,那就可以了.
归档时间: |
|
查看次数: |
3365 次 |
最近记录: |