我已經學習了Objective-C五天,並且我只有2周的編程經驗,所以請儘可能簡單地做出答案。嵌套的NSArray循環不會循環嵌套時
我在一本書中做了一個練習,要求我生成也是普通單詞的專有名稱列表。爲此,我正在爲NSArray
專有名稱對象中的每個專有名稱運行for循環。在該循環中,我使用caseInsensitiveCompare
方法,使用嵌套的for循環測試NSArray
普通單詞對象中每個單詞的每個單詞。
這裏是我的代碼:
import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//Gets the sting with proper names
NSString *propername = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:
NSUTF8StringEncoding error:NULL];
//Gets the string with regularwords
NSString *inpropername = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:
NSUTF8StringEncoding error:NULL];
NSArray *proper = [propername componentsSeparatedByString:@"/n"];
NSArray *inproper = [inpropername componentsSeparatedByString:@"/n"];
for (NSString *n in proper){
NSLog(@"%@", n);
for(NSString *i in inproper){
NSLog(@"%@", i);
if ([n caseInsensitiveCompare:i] == NSOrderedSame)
{
NSLog(@"Yahooo! Got One! %@", n);
}
}
}
}
return 0;
}
取而代之的是在它們以順序方式運行的嵌套的方式運行循環。輸出是這樣的:
Aaron
all the names...
Yvonne
a
all the regular words....
Zyzzogeton
任何想法爲什麼嵌套for循環不是以嵌套方式運行?
我的猜測是你沒有向我們展示一些東西。 – 2012-08-12 01:44:49
我剛剛添加了我的所有代碼 – ChemDev 2012-08-12 01:52:10
「/ n」是可疑的...我想你的意思是「\ n」 – Julien 2012-08-12 02:03:22