美文网首页
gcd实现和for循环一样的快速迭代

gcd实现和for循环一样的快速迭代

作者: AntKing | 来源:发表于2017-04-28 16:43 被阅读0次

使用gcd能够实现和for循环一样的快速迭代,而且还是在多线程里执行的,效率应该是相当的高的,代码列子如下

  • 下面的列子我们使用gcd和for循环都来实现一个移动文件的操作
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self moveFileWithGCD];
}

-(void)forDemo
{
    //同步
    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 *from = @"/Users/xiaomage/Desktop/from";
    
    //2.获得目标文件路径
    NSString *to = @"/Users/xiaomage/Desktop/to";
    
    //3.得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    
    NSLog(@"%@",subPaths);
    //4.遍历所有文件,然后执行剪切操作
    NSInteger count = subPaths.count;
    
    for (NSInteger i = 0; i< count; i++) {
        
        //4.1 拼接文件的全路径
       // NSString *fullPath = [from stringByAppendingString:subPaths[i]];
        //在拼接的时候会自动添加/
        NSString *fullPath = [from 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]);
    }
}




-(void)moveFileWithGCD
{
    //1.拿到文件路径
    NSString *from = @"/Users/xiaomage/Desktop/from";
    
    //2.获得目标文件路径
    NSString *to = @"/Users/xiaomage/Desktop/to";
    
    //3.得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    
    NSLog(@"%@",subPaths);
    //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]];
        
        NSLog(@"%@",fullPath);
        //4.2 执行剪切操作
        /*
         第一个参数:要剪切的文件在哪里
         第二个参数:文件应该被存到哪个位置
         */
        [[NSFileManager defaultManager]moveItemAtPath:fullPath toPath:toFullPath error:nil];
        
        NSLog(@"%@---%@--%@",fullPath,toFullPath,[NSThread currentThread]);

    });
}
@end

相关文章

  • gcd实现和for循环一样的快速迭代

    使用gcd能够实现和for循环一样的快速迭代,而且还是在多线程里执行的,效率应该是相当的高的,代码列子如下 下面的...

  • 五. GCD的快速迭代

    GCD的快速迭代 通常我们进行遍历, 都是使用的for循环或者while循环但是, 普通的循环, 是在主线程中执行...

  • GCD的快速迭代

    GCD的快速迭代 通常我们进行遍历, 都是使用的for循环或者while循环 但是, 普通的循环, 是在主线程中执...

  • GCD快速迭代

    普通for循环遍历数据 使用for循环遍历数据,是同步的, 串行的 GCD快速迭代: 开子线程和主线程一起完成遍历...

  • GCD相关方法

    1.gcd栅栏函数 2.gcd快速迭代方法(dispatch_apply)同for循环做比较。 案例:将文件夹fr...

  • NSMutableArray的遍历及删除

    for循环、快速循环forin、迭代器 NSEnumerator、迭代器的block形式 [array enume...

  • iOS面试--GCD常见用法

    项目中常见的GCD用法有已下几种: 1.GCD栅栏函数2.GCD快速迭代(遍历)3.GCD队列组的使用 1.GCD...

  • iOS开发GCD遍历(迭代)和For循环分析

    『导言』 iOS开发中经常用到遍历数据用到for循环,其实GCD中有个更好用的方法来快速迭代,下面我们来看看两者的...

  • 二分查找的难点

    二分查找有两种实现方式:递归实现,循环迭代实现,相比而言,迭代循环实现比较有难度 递归实现方式 迭代实现方式 二者...

  • 控制流:迭代循环(for)

    控制流:迭代循环(for) 什么是迭代循环 迭代循环语句 Python语言中的for语句实现了循环结构的 第一种循...

网友评论

      本文标题:gcd实现和for循环一样的快速迭代

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