2012-11-01 70 views
3

在我的應用程序使用的NSFileManager使用獲得的文件數量的文件夾在下面的代碼.DS_Store文件跳過

NSFileManager *manager=[NSFileManager defaultManager]; 
NSString *path; 
int numberofFiles=[[manager contentsOfDirectoryAtPath:path error:nil] count]; 
numberofFiles=numberofFiles-1; //number of files except .DS_Store 

但我的問題是,該文件.DS_Store並不總是defaultly創建的,當時我得到的數量少於目錄中實際存在的文件數量。

那麼,有沒有在的NSFileManager的方法,它返回排除.DS_Store 文件的數組或我使用-IsEqualToString方法 手動排除,否則沒有任何選項來創建無.DS_Store文件的新目錄。

+0

你可以存儲'[經理contentsOfDirectoryAtPath:路徑error:nil]'放入'NSArray'並使用[NSPredicate](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html)過濾掉.DS_Store。 – hellozimi

回答

2

明確查找.DS_Store文件和調整數,如果它的發現:

NSFileManager *manager=[NSFileManager defaultManager]; 
NSString *path = ...;  // Presumably this is a valid path? 
NSArray *contents = [manager contentsOfDirectoryAtPath:path error:nil]; 
NSUInteger numberOfFiles = [contents count]; 
if ([contents indexOfObject:@".DS_Store"] != NSNotFound) 
    numberOfFiles--; 
0

試試這個。它的工作對我來說..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSFileManager *manager = [NSFileManager defaultManager]; 
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

for (int i = 0; i < [imageFilenames count]; i++) 
    { 

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ]; 

     if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"]) 
     { 
      UIImage *myimage = [UIImage imageWithContentsOfFile:imageName]; 
      UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage]; 
     } 
    }