Ios5 iOS在NSString中迭代

Ios5 iOS在NSString中迭代,ios5,nsstring,loops,Ios5,Nsstring,Loops,迭代NSString的最佳方法是什么?代码如下: int len = str.length; for (int i = 0; i < len; ++i) { unichar c = [str characterAtIndex:i]; // do something with c } 有没有比我的第一段代码更优雅的方法 for(NSString *subString in yourString){ NSLog("The Substring is: %@", subS

迭代NSString的最佳方法是什么?代码如下:

int len = str.length;
for (int i = 0; i < len; ++i)
{
    unichar c = [str characterAtIndex:i];
    // do something with c
}
有没有比我的第一段代码更优雅的方法

for(NSString *subString in yourString){
    NSLog("The Substring is: %@", subString);
    // do anything you want with the substring in this loop
}

这必须起作用(如果不起作用,您可以将字符串定义为
NSDictionary
,然后重试)

这不起作用。NSString不响应“countByEnumeratingWithState:objects:count:”。您试图对该字符串执行什么操作?有很多内置函数可以让你跳过简单的字符串迭代。如果有人像我一样进入这个页面,请检查这个
for(NSString *subString in yourString){
    NSLog("The Substring is: %@", subString);
    // do anything you want with the substring in this loop
}