【问题标题】:how to read files of a directory?如何读取目录的文件?
【发布时间】:2011-08-26 05:03:42
【问题描述】:
FM = [NSFileManager defaultManager];
directoryContents = [FM contentsOfDirectoryAtPath:@"/Users/myComputer/Library/Application Support/iPhone Simulator/4.3/Applications/CD88649C-E545-4E10-84DF-F4E5D829641B/Documents" error:NULL];
但它给了我对象而不是对象的路径。
【问题讨论】:
标签:
iphone
objective-c
nsfilemanager
【解决方案1】:
我发现了如何读取存储在目录中的文件。通过使用:
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil]
它返回存储在该位置的文件或文件夹名称。然后我们可以再次将这些名称附加到一个字符串中以读取这些文件。
【解决方案2】:
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
for (NSString *tString in dirContents)
{
//Do some stuff here
}
看到这个帖子Get directory contents in date modified order
【解决方案3】:
我真的不明白下面用于获取捆绑路径的捆绑路径的硬编码字符串
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
您的代码必须与以下类似。
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];