A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© weizhang00 中级黑马   /  2014-12-1 18:29  /  1451 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //
  2. //  NSString+Tools.m
  3. //  FileLineNumbers
  4. //
  5. //  Created by 疯兔 on 14/12/1.
  6. //  Copyright (c) 2014年 Mr.z. All rights reserved.
  7. //
  8. //本代码用于快速统计代码行数
  9. #import "NSString+Tools.h"
  10. #define HFILE @"h"
  11. #define MFILE @"m"
  12. #define CFILE @"c"
  13. #define CODECOMMENTS @"//"
  14. #define CODECOMMENTSTYPEBEGIN @"/*"
  15. #define CODECOMMENTSTYPEEND @"*/"
  16. /*
  17. 本程序可统计.h/.m/.c文件类型
  18. 可识别文件中的注释行数,但未进行有效注释计算
  19. 可识别无效行数
  20. 返回值为有效代码行数,包含全部注释行数
  21. 注意:不接受URL路径
  22. */
  23. @implementation NSString (Tools)


  24. + (NSUInteger)stringFileLineNumbers:(NSString *)path
  25. {
  26.     BOOL isDir = NO;
  27.     NSFileManager *fileManager = [NSFileManager defaultManager];
  28.     BOOL isRightPath = [fileManager fileExistsAtPath:path isDirectory:&isDir];//返回路径是否存在BOOL,传入为是否文件夹BOOL
  29.    
  30.     if (!isRightPath) {
  31.         NSLog(@"无法识别路径");
  32.         return 0;
  33.     }
  34.    
  35.     if (isDir) {
  36.         
  37.         __block int count = 0;
  38.         NSArray *dirFiles = [fileManager contentsOfDirectoryAtPath:path error:Nil];
  39.         [dirFiles enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  40.             NSString *dirFilePath = [NSString stringWithFormat:@"%@/%@",path,obj];
  41.             count +=   [NSString stringFileLineNumbers:dirFilePath];
  42.         }];
  43.         
  44.         return count;
  45.     }else{
  46.         
  47.         //取出扩展名,并进行小写转化
  48.         NSString *pathEx = [[path pathExtension]lowercaseString];
  49.         if (![pathEx isEqualToString:HFILE]
  50.             &&
  51.             ![pathEx isEqualToString:MFILE]
  52.             &&
  53.             ![pathEx isEqualToString:CFILE]) {
  54.             return 0;
  55.         }
  56.         
  57.         
  58.         NSArray *getFileName = [path componentsSeparatedByString:@"/"];
  59.         NSString *fileName = [getFileName lastObject];
  60.         
  61.         //1.加载文件内容
  62. //        NSLog(@"开始加载:%@文件",fileName);
  63.         
  64.         NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:Nil];
  65.         NSArray *contendArray = [content componentsSeparatedByString:@"\n"];//2.分割字符串
  66.         
  67.         __block int noCode = 0;//无效行数
  68.         __block int comments = 0;//注释行数
  69.         __block BOOL isComments = NO;
  70.         
  71.         [contendArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  72.             NSString * stringObj = obj;
  73.             if (stringObj.length < 1) {
  74.                 noCode++;
  75.             }
  76.             
  77.             if ([stringObj rangeOfString:CODECOMMENTS].location != NSNotFound) {
  78.                 comments++;
  79.             }
  80.             if ([stringObj rangeOfString:CODECOMMENTSTYPEBEGIN].location != NSNotFound) {
  81.                 isComments = YES;
  82.             }
  83.             if ([stringObj rangeOfString:CODECOMMENTSTYPEEND].location != NSNotFound) {
  84.                 isComments = NO;
  85.             }
  86.             if (isComments) {
  87.                 comments++;
  88.             }
  89.             
  90.             
  91.         }];
  92.         NSUInteger codeCount = contendArray.count - noCode;
  93.         NSLog(@"文件名:%@中有效代码行数:%ld,无效代码行数:%d,注释行数:%d,总行数:%ld",fileName,codeCount,noCode,comments,contendArray.count);
  94.         return contendArray.count - noCode;
  95.     }
  96.    
  97. }
  98. + (void)dateLog
  99. {
  100.     NSDate *date = [NSDate date];
  101.     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  102.     formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  103.     NSString *dateString = [formatter stringFromDate:date];
  104.     NSLog(@"本次统计开始时间为  %@",dateString);
  105. }
  106. @end
复制代码


4 个回复

倒序浏览
最下面的DATE方法是开始想多了,NSLog自带时间,没有用的方法
回复 使用道具 举报
写的很不错。。。
回复 使用道具 举报
看着 都蛋疼
回复 使用道具 举报
赞一个,很不错,向大神学习;
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马