2
A
回答
8
試試這個:
//Retrieve an array of paths in the given directory
NSArray *imagePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: @"/Images/" error: NULL];
if(imagePaths==nil || [imagePaths count]==0) {
//Path either does not exist or does not contain any files
return;
}
NSEnumerator *enumerator = [imagePaths objectEnumerator];
id imagePath;
NSString *newImageName;
NSMutableArray *imageNames = [[NSMutableArray alloc] init];
while (imagePath = [enumerator nextObject]) {
//Remove the file extension
newImageName = [imagePath stringByDeletingPathExtension];
[imageNames addObject:newImageName];
}
代碼的第一部分檢索給定目錄中文件的名稱,並將它們添加到imagePaths
陣列中。
代碼的第二部分則循環通過陣列中的圖像路徑和從端部(使用stringByDeletingPathExtension
方法)去除擴展,將它們附加到imageNames
陣列,其中將包含在給定目錄中的所有文件沒有任何文件擴展名。
由於Graham Lee
已在他的代碼中完成,因此您可以提供一個指向NSError
對象的指針,以便在出現錯誤時獲取有關該問題的信息。
4
NSError *error = nil;
NSArray *imageFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: @"/path/to/Images/" error: &error];
if (imageFiles == nil) //handle the error
相關問題
- 1. 獲取文件的擴展名,但無法獲取文件名
- 2. 如何從NSArray獲取名稱
- 3. 獲取文件名
- 4. 獲取文件名
- 5. 獲取文件名
- 6. 從NSArray獲取單個屬性的NSArray
- 7. 獲取文件的名稱
- 8. 從NSArray獲取NSString
- 9. 獲取NSArray鍵值
- 10. 獲取NSArray JSON值
- 11. 獲取加密文件的文件名
- 12. 獲取PHP文件的文件名
- 13. 獲取文件路徑的文件名
- 14. 獲取整數值的NSArray
- 15. 從NSArray中獲取的NSIndexSet
- 16. 獲取NSArray中的xcode項目文件夾的所有圖像
- 17. 7zip - 獲取提取文件的名稱
- 18. 獲取文件名批量
- 19. ASP.NET:獲取ASPX文件名
- 20. 獲取BPL文件名
- 21. 動態獲取文件名
- 22. ASPxUploadControl FileUploadComplete獲取文件名
- 23. jwplayer 6:獲取文件名
- 24. 從BufferedImage獲取文件名
- 25. 如何獲取文件名
- 26. 獲取文件擴展名
- 27. 獲取文件名C#
- 28. 獲取文件夾名稱
- 29. 獲取文件名到myXhr.upload
- 30. 獲取文件夾名稱
在我有限的經驗中,這比追蹤.plist好一百萬倍。 – buildsucceeded 2011-10-11 20:14:57