文件-沙盒目录代码段;

本文介绍了一个iOS应用中用于文件复制的方法,包括从资源文件夹复制文件到应用的文档目录,并检查目标文件是否存在以避免重复复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma mark -把文件复制到沙盒目录;
- (BOOL)copyFilePath:(NSString *)fromPath toPath:(NSString *)toPath{

    NSFileManager *fileMan = [NSFileManager defaultManager];
    NSError *error = nil;
    if ([fileMan fileExistsAtPath:toPath]) {
        debugLog(@"MDRootVC:The file %@ is exist!", toPath);
        return NO;
    }
    BOOL retCode = [fileMan copyItemAtPath:fromPath toPath:toPath error:&error];
    return retCode;
}

categoryArray = [NSMutableArray arrayWithCapacity:1];
    NSString *docPth = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *toPth = [docPth stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", DM_CATEGORY_ARR]];
    /*
    NSString *path111 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths1 objectAtIndex:0];
    NSLog(@"app_home_lib: %@",libraryDirectory);
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"app_home_doc: %@",documentsDirectory);
    */
    
    NSFileManager *fileMana = [NSFileManager defaultManager];
    if ([fileMana fileExistsAtPath:toPth]) {
        debugLog(@"这个文件已经存在,不要再复制了,程序第一次启动时已经复制了");
        categoryOriginArr = [[NSArray alloc] initWithContentsOfFile:toPth];
        
    }else{
        NSString *catPth = [[NSBundle mainBundle] pathForResource:DM_CATEGORY_ARR ofType:@"plist"];
        categoryOriginArr = [NSArray arrayWithContentsOfFile:catPth];
        [self copyFilePath:catPth toPath:toPth];  //这个函数里面已经有文件存在与否的判断;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值