2012-05-16 40 views
0

我可以驗證是否在我的文件夾NSHomeDirectory中的「文檔」有沒有或至少有一個文件?IOS:驗證一個空的NSHomeDirectory

NSError *error; 
NSFileManager *fileMgr = [NSFileManager defaultManager]; 

// Point to Document directory 
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

// Write out the contents of home directory to console 
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

回答

2

當然!

NSFileManager的contentsOfDirectoryAtPath: error:方法返回一個數組。

如果它是一個數爲零的數組,它是一個空目錄。

如果它返回一個錯誤,那麼該目錄不存在(或者你傳入的路徑錯誤)。