2012-08-12 134 views
0

我已經學習了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循環不是以嵌套方式運行?

+0

我的猜測是你沒有向我們展示一些東西。 – 2012-08-12 01:44:49

+0

我剛剛添加了我的所有代碼 – ChemDev 2012-08-12 01:52:10

+3

「/ n」是可疑的...我想你的意思是「\ n」 – Julien 2012-08-12 02:03:22

回答

4

該代碼是正確的,除非您不是將文件分解爲單詞,因爲您使用的是「/ n」而不是「\ n」。

這意味着每個數組只包含一個元素,它是一個包含所有單詞的字符串。

+0

你完全正確!謝謝! – ChemDev 2012-08-12 02:20:09