#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self moveFileWithGCD];
}
-(void)forDome
{
//同步
for (NSInteger i = 0; i<10; i++) {
NSLog(@"%zd---%@",i,[NSThread currentThread]);
}
}
//开子线程和主线程一起完成遍历任务,任务的执行时并发的
- (void)applyDemo
{
/*
第一个参数:遍历的次数
第二个参数:队列(并发队列)
第三个参数:index 索引
*/
dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"%zd---%@",index,[NSThread currentThread]);
});
}
//使用for循环
- (void)moveFile{
//1.拿到文件路径
NSString *form = @"/Users/liuzhenjie/Desktop/from";
//2.获取目标文件路劲
NSString *to = @"/Users/liuzhenjie/Desktop/to";
// 3.得到目录下面的所有文件
NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:form];
NSLog(@"%@",subPaths);
//4.遍历所有文件,然后执行剪切操作
NSInteger count = subPaths.count;
for (NSInteger i=0; i<count; i++) {
//4.1拼接文件的全路径
// NSString *fullPath = [form stringByAppendingString:subPaths[i]];
NSString *fullPath = [form stringByAppendingPathComponent:subPaths[i]];
NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];
NSLog(@"%@",fullPath);
// 4.2执行剪切操作
/**
* 第一个参数:要剪切的文件在哪里
// 第二个参数:文件应该被存到那个位置
*/
[[NSFileManager defaultManager]moveItemAtPath:fullPath toPath:toFullPath error:nil];
NSLog(@"%@---%@--%@",fullPath,toFullPath,[NSThread currentThread]);
}
}
//使用GCD
-(void)moveFileWithGCD
{
//1.拿到文件路径
NSString *from = @"/Users/liuzhenjie/Desktop/from";
//2.获得目标文件路径
NSString *to = @"/Users/liuzhenjie/Desktop/to";
//3.得到目录下面的所有文件
NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
//4.遍历所有文件,然后执行剪切操作
NSInteger count = subPaths.count;
dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {
//4.1 拼接文件的全路径
// NSString *fullPath = [from stringByAppendingString:subPaths[i]];
//在拼接的时候会自动添加/
NSString *fullPath = [from stringByAppendingPathComponent:subPaths[i]];
NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];
//4.2 执行剪切操作
/*
第一个参数:要剪切的文件在哪里
第二个参数:文件应该被存到哪个位置
*/
[[NSFileManager defaultManager]moveItemAtPath:fullPath toPath:toFullPath error:nil];
NSLog(@"%@---%@---%@",fullPath,toFullPath,[NSThread currentThread]);
});
}
@end