• 遍历


    1、快速遍历

    for(id obj in array)

    {

      //找出obj元素在数组中的位置

    NSUInteger i = [array indexOfObject:obj];

    }

    2、Block遍历

    //每遍历到一元素,就会调用一次block,并且当前元素和索引位置当做参数传给block

    [array enumerateObjectsUsingBlock: ^(id obj,NSUInteger idx ,BOOL *stop){

        NSLog(@"%ld--%@",idx,obj);

        if(idx == 0)

    {

        //停止遍历,到0就停止,即遍历一次

    *stop = YEX;

    }

    }];

    例子:计算代码行数

    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];//加载文件内容

    NSArray *array= [content componentsSepatatedByString:@" "];//用" “分割代码成数组

    最后计算数组的个数

    计算文件夹下的所有文件的所有代码行数

    NSFileManager *mgr = [NSFileManager deaultManager];//获得文件管理者

    BOOL dir = NO;//标记是否为文件夹

    BOOL exist = [mgr fileExistsAtPath:path isDirectory:&dir];//判断path这个路径是否存在

    if(dir)//如果存在

    {

      NSArray *array = [mgr contents0fDirectoryAtPath:path error:nil];//装着当前文件夹下面的所有内容(文件夹,文件)

    int count = 0;

    for(NSString *filename in array)//遍历数组中的所有文件(夹)名

    {

     NSString *fullpath = [NSString stringWithFormat:@"%@/%@",path,filename];//子文件(夹)的全路径

    cout += codeLineCount(fullPath);//累加每个子路径的总行数

    }

    return count;

    }

    else//dir不存在

    {

    。。。。。

    }

  • 相关阅读:
    .NET——编写一个计算器
    利用Jieba对txt进行分词操作并保存在数据库中
    软件设计——代理模式之婚介所
    不单单要学程序,也要学穿衣服
    第一篇,就写今天看的东西
    Python中的参数传递问题
    LINUX基础内容
    python中时间相关问题,仅作为笔记
    appium自动化测试
    HTTP协议返回状态码
  • 原文地址:https://www.cnblogs.com/zhongxuan/p/4848798.html
Copyright © 2020-2023  润新知