2012-08-12 137 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