2013-02-08 79 views
1

我试图寻找的plist这里命名为1至6个香港专业教育学院写了这个代码和它使应用程序消耗3秒钟加载,然后原来的时间....代码文件搜索花费过多时间

for (int i=1;i<6;i++) { 
    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 
    NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:bundleRoot]; 
    for (NSString *tString in dirContents) { 
     if ([tString hasPrefix:[NSString stringWithFormat:@"%d",i]] && [tString hasSuffix:@".plist"]) { 
      NSLog(@"file found"); 
      NSString *plist = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"plist"]; 
      mute = [NSMutableArray arrayWithContentsOfFile:plist]; 
      [mute addObjectsFromArray:contentArray]; 
      contentArray = mute; 
     } 
     else { 
      NSLog(@"not found"); 
     } 
    } 
} 

能有人打我一个解决方案或定义什么是错在这里

+1

循环将不会运行6次它将只运行5次:) – 2013-02-08 11:33:54

+0

+1 @LegendReborn :)(尽管你可能只是让他的代码慢了20%哈哈)! – deanWombourne 2013-02-08 11:34:51

+0

我只想让它跑5次...写了6错误.... – 2013-02-08 11:37:06

回答

1

这并不是非常有效的:)

试试这个:

for (int i = 1; i <= 6; ++i) { 
    // Load the plist 
    NSString *plist = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"plist"]; 
    if (nil == plist) { 
    NSLog(@"not found (%i)", i); 
    continue; 
    } 

    // Do something with your plist 
    mute = [NSMutableArray arrayWithContentsOfFile:plist]; 
    [contentArray addObjectsFromArray:mute]; 
} 

如果你仍然得到3秒的延迟,那么这不是搜索的问题,它是plists的大小和你需要做的处理。当你加载它们时,你可能只需要放一个'请稍等'的信息和一个微调器。

或者你可以尝试加载它们在后台线程,所以你的用户界面仍然有效。我想这取决于你的应用程序中发生了什么!

+0

感谢很多家伙...我们的代码很好.... – 2013-02-08 11:38:30

+0

但它仍然需要太多的时间来加载,plist已50个条目可能是这个问题..每个条目都是一个笑话,包含100个单词 – 2013-02-08 11:39:29

+0

嗯,如果你制作了contentArray NSMutableArray,你可以让每个plist在那里_slightly_更快(见我的编辑)。虽然我怀疑这会有很大的不同。需要多长时间? – deanWombourne 2013-02-08 11:50:22