网络多线程 - 01-掌握-GCD快速迭代


#import"ViewController.h"


@interface ViewController()


@end


@implementation ViewController


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [selfMoveFileApply];

}

-(void)MoveFile

{

    //1.要剪切的文件夹路径

   NSString*from =@"/Users/a1/Desktop/from";

    

   //2.目标文件夹的路径

   NSString*to =@"/Users/a1/Desktop/to";

    

   //3.文件管理者

   NSFileManager*manager = [NSFileManagerdefaultManager];

    

    NSArray *subPaths = [manager subpathsAtPath:from];

    

    NSLog(@"%@",subPaths);

    //4.遍历文件并执行剪切文件的操作

    NSInteger count = subPaths.count;

    for (NSInteger i = 0; i<count; i++) {

        

       //4.0 文件的名称

        NSString *fileName = subPaths[i];

        

       //4.1 拼接文件的全路径

        NSString *subPath = [from stringByAppendingPathComponent:fileName];

       //        /Users/a1/Desktop/from/Snip20151124_16.png

        

       //4.2 应该剪切到什么地方去

        NSString *fullPath = [to stringByAppendingPathComponent:fileName];

       //        /Users/a1/Desktop/to/Snip20151124_16.png

        

       //4.3 执行剪切

        /*

         第一个参数:在什么地方

         第二个参数:要移到哪里

         第三个参数:nil

         */

        [manager moveItemAtPath:subPath toPath:fullPath error:nil];

        

        NSLog(@"%@--%@,%@",subPath,fullPath,[NSThread currentThread]);

    }

}

-(void)MoveFileApply

{

    //1.要剪切的文件夹路径

   NSString*from =@"/Users/a1/Desktop/from";

    

   //2.目标文件夹的路径

   NSString*to =@"/Users/a1/Desktop/to";

    

   //3.文件管理者

   NSFileManager*manager = [NSFileManagerdefaultManager];

    

    NSArray *subPaths = [manager subpathsAtPath:from];

    

    NSLog(@"%@",subPaths);

    //4.遍历文件并执行剪切文件的操作

    NSInteger count = subPaths.count;

    

   dispatch_apply(count,dispatch_get_global_queue(0,0), ^(size_tindex) {

       //4.0 文件的名称

        NSString *fileName = subPaths[index];

        

       //4.1 拼接文件的全路径

        NSString *subPath = [from stringByAppendingPathComponent:fileName];

       //        /Users/a1/Desktop/from/Snip20151124_16.png

        

       //4.2 应该剪切到什么地方去

        NSString *fullPath = [to stringByAppendingPathComponent:fileName];

       //        /Users/a1/Desktop/to/Snip20151124_16.png

        

       //4.3 执行剪切

        /*

         第一个参数:在什么地方

         第二个参数:要移到哪里

         第三个参数:nil

         */

        [manager moveItemAtPath:subPath toPath:fullPath error:nil];

        

        NSLog(@"%@--%@,%@",subPath,fullPath,[NSThread currentThread]);

    });

    

}



-(void)test

{

    //迭代

    /*

     for (NSInteger i = 0; i<10; i++) {

     NSLog(@"%zd---%@",i,[NSThread currentThread]);

     }

     */

    

   //GCD中快速迭代

    /*

    第一个参数:要遍历的次数

    第二个参数:队列(并发

     第三个参数:size_t 索引

     */

   dispatch_apply(10,dispatch_get_global_queue(0,0), ^(size_tindex) {

        NSLog(@"%zd---%@",index,[NSThread currentThread]);

    });

}

@end



版权声明:本文为VAN_MR原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。