Objective c iOS列出现有目录
我已经得到了文档目录的路径,并在其中创建了一些目录。我已经知道如何检查目录是否存在,删除它或它的文件,但是,我怎么能列出目录?多谢各位 对于文件列表,我使用:Objective c iOS列出现有目录,objective-c,ios,xcode,cocoa-touch,Objective C,Ios,Xcode,Cocoa Touch,我已经得到了文档目录的路径,并在其中创建了一些目录。我已经知道如何检查目录是否存在,删除它或它的文件,但是,我怎么能列出目录?多谢各位 对于文件列表,我使用: int Count; NSString *path; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); path = [[paths objectAtIndex:0] s
int Count;
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (Count = 0; Count < (int)[directoryContent count]; Count++)
{
NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]);
}
int计数;
NSString*路径;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
path=[[paths objectAtIndex:0]stringByAppendingPathComponent:@“SomeDirectoryName”];
NSArray*目录内容=[[NSFileManager defaultManager]目录目录路径:路径错误:NULL];
对于(计数=0;计数<(int)[directoryContent计数];计数++)
{
NSLog(@“文件%d:%@,(计数+1),[directoryContent对象索引:计数]);
}
例如,此方法从应用程序的临时目录中删除所有文件:
- (void)cleatTmpDirectory
{
// Create a local file manager instance
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSURL *directoryToScan = [NSURL fileURLWithPath:[self applicationTmpDirectory]];
NSDirectoryEnumerator *dirEnumerator =
[[localFileManager enumeratorAtURL:directoryToScan
includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLIsDirectoryKey,nil]
options: NSDirectoryEnumerationSkipsHiddenFiles |
NSDirectoryEnumerationSkipsSubdirectoryDescendants |
NSDirectoryEnumerationSkipsPackageDescendants
errorHandler:nil] retain];
NSError *error;
// Enumerate the dirEnumerator results, each value is stored in allURLs
for (NSURL *theURL in dirEnumerator)
{
// Retrieve whether a directory.
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if ([isDirectory boolValue] == NO)
{
[localFileManager removeItemAtURL:theURL error:&error];
}
}
// Release the localFileManager.
[localFileManager release];
}
正如您所发现的,您应该使用
NSDirectoryEnumerator*dirEnumerator
并将相应的键传递给其初始化方法,然后使用。使用NSFileManager的-EnumeratorPath:
方法返回的NSDirectoryEnumerator。您能指导我怎么做吗?@AmiriDev您看到了吗?这里有一个例子,你能回答这个问题吗。