iOS NSFileManager 的简单使用

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/chen12302asd/article/details/81906883

最进项目需要访问iOS的沙盒目录,学习了一些项目中用到的方法,记录下来方便以后查询。

访问Documents目录

自己写了一个方法来获取该目录,参照了iOS提供的方法,采用了C语言的语法

//系统的头文件中定义的方法,NSPathUtilities.h。
FOUNDATION_EXPORT NSString *NSTemporaryDirectory(void);
//自己写的获取Documents目录的方法。
NSString * NSDocumentsDirectory(void) {
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
}

添加文件

- (void)addFiles{
    int i;
    for (i = 0; i<66; i++) {
        NSString *path = NSDocumentsDirectory();
        NSString *test = @"Hello world!";
        NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"test%d.xml", i+1]];
        [test writeToFile:filePath atomically:true encoding:NSUTF8StringEncoding error:nil];
    }
}

这个方法向Documents目录下添加一些xml文件,项目中的xml文件是从ftp服务器下载的,这里偷个懒,关于从服务器下载xml文件的方法,在其他文章进行了说明。

显示文件列表

使用tableView显示Documents中的文件。完整的代码如下:

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>{
    NSFileManager *manager;
}
@property (weak, nonatomic) IBOutlet UILabel *pathLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataList;

@end

@implementation ViewController

NSString * NSDocumentsDirectory(void) {
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
}

- (void)addFiles{
    int i;
    for (i = 0; i<66; i++) {
        NSString *path = NSDocumentsDirectory();
        NSString *test = @"Hello world!";
        NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"test%d.xml", i+1]];
        [test writeToFile:filePath atomically:true encoding:NSUTF8StringEncoding error:nil];
    }
}

- (void)deleteFiles{
    NSArray *files = [manager contentsOfDirectoryAtPath:NSDocumentsDirectory() error:nil];
    for (NSString *path in files) {
        if ([path hasSuffix:@"xml"]) {
            [manager removeItemAtPath:[NSDocumentsDirectory() stringByAppendingPathComponent:path] error:nil];
        }
    }
}

- (void)showFileList{
    [self.dataList removeAllObjects];
    NSArray *array = [manager contentsOfDirectoryAtPath:NSDocumentsDirectory() error:nil];
    for(NSString *path in array){
        [self.dataList addObject:path];
    }
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSTemporaryDirectory();
    self.pathLabel.text = NSDocumentsDirectory();
    NSLog(@"%@", self.pathLabel.text);
    manager = [NSFileManager defaultManager];
    self.dataList = [[NSMutableArray alloc] init];
    [self.tableView registerClass:[UITableViewCell classForCoder] forCellReuseIdentifier:@"cells"];
}

- (IBAction)add:(id)sender {
    [self deleteFiles];
    [self addFiles];
    [self showFileList];
}


- (IBAction)delete:(id)sender {
    [self deleteFiles];
    [self showFileList];
}

#pragma mark - UITableViewDelegate

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells"];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.textLabel.text = self.dataList[indexPath.row];
    return cell;
}

图片来自网络

猜你喜欢

转载自blog.csdn.net/chen12302asd/article/details/81906883