美文网首页
剪切文件

剪切文件

作者: 木子小静 | 来源:发表于2020-05-11 15:50 被阅读0次

快速迭代文件剪切

- (void)apply
{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    NSString *from = @"剪切前的路径";
    NSString *to = @"剪切后的路径";
    
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *subpaths = [manager subpathsAtPath:from];
    
    // GCD的快速迭代,也可用for循环代替,但是效率比for快
    dispatch_apply(subpaths.count, queue, ^(size_t index) {
        
        NSString *subpath = subpaths[index];
        NSString *fromFullPath = [from stringByAppendingPathComponent:subpath];
        NSString *toFullPath = [to stringByAppendingPathComponent:subpath];
        
        // 剪切
        [manager moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
    });
}

相关文章

网友评论

      本文标题:剪切文件

      本文链接:https://www.haomeiwen.com/subject/cpspettx.html