2013-02-08 78 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