码迷,mamicode.com
首页 > 编程语言 > 详细

多线程 CGD快速迭代

时间:2016-02-20 15:53:12      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 

#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

多线程 CGD快速迭代

标签:

原文地址:http://www.cnblogs.com/liuzhenjie/p/5203292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!