iPhone - 文件列表的属性
我有一个 mutableArray,它在 tableView 中显示文件列表。我已将这些文件存储在一个目录中。
我正在使用 NSfileManager
类和 attributesOfItemAtPath
方法来检索文件的一些属性,例如 fileSize、fileType..
在第二阶段,我尝试拥有一个显示详细信息的控制器在 tableView 中触摸时特定文件的属性。
问题是我不知道如何将 fileSize 和 fileType 等属性分别绑定到 NSDictionary 并使其显示在该特定文件的详细视图中。
这是我用于列出文件和属性的源代码。
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
directoryContent = [[NSMutableArray alloc] init];
for (NSString *path in paths){
NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
[directoryContent addObject:documentsDirectory];
}
}
-(void) willProcessPath {
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
for (NSString *path in paths){
filesPath = [parentDirectory stringByAppendingPathComponent:path];
NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
filesSize = [attrDir objectForKey:NSFileSize];
filesName = (NSString *)directoryContent;
filesType = [path pathExtension];
createdDate = [attrDir objectForKey:NSFileCreationDate];
modifiedDate = [attrDir objectForKey:NSFileModificationDate];
}
}
请帮助我继续下一步..提前致谢..
I have an mutableArray which displays a list of files in a tableView. I have stored those files in a directory.
I am using NSfileManager
class and attributesOfItemAtPath
method to retrieve some properties of the files like fileSize, fileType..
In the second stage I am trying to have a detailView controller which displays the properties of the particular file when touched in the tableView.
The problem is I dont know how to bind those properties like fileSize and fileType separately to a NSDictionary
and make it display in the detailView for that particular file.
Here is my source code for listing the files and to list the properties.
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
directoryContent = [[NSMutableArray alloc] init];
for (NSString *path in paths){
NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
[directoryContent addObject:documentsDirectory];
}
}
-(void) willProcessPath {
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
for (NSString *path in paths){
filesPath = [parentDirectory stringByAppendingPathComponent:path];
NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
filesSize = [attrDir objectForKey:NSFileSize];
filesName = (NSString *)directoryContent;
filesType = [path pathExtension];
createdDate = [attrDir objectForKey:NSFileCreationDate];
modifiedDate = [attrDir objectForKey:NSFileModificationDate];
}
}
Please help me to proceed to the next step.. Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样的事情:
Try something like this:
为什么不创建一个具有以下属性的“FileDescriptor”类:
在遍历目录时,为每个文件创建此类的一个实例,并将其存储在数组(或字典,如果您愿意)中。然后,您可以使用该数组
a) 为 UITableViewCells 提供数据
b) 单击单元格时显示每个文件的详细视图。
如果你想使用字典(我认为这不太“干净”),你可以这样做:
Why not create a class "FileDescriptor" with the following properties:
When iterating over your directory, you create an instance of this class for each file, and store that in an array (or a dictionary, if you like). Then you can use that array to
a) provide data for your UITableViewCells
b) show a detail view for each file when the cell is clicked.
Should you want to use a dictionary instead (which I think is a lot less "clean") you can do so like this: