标签:
1.全局变量:
@property (nonatomic, assign) BOOL isPicUsed;
2.方法:
- (NSInteger)codeLinesWithPath:(NSString *)path{
NSFileManager *fileManaget = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isExist = [fileManaget fileExistsAtPath:path isDirectory:&isDir];
if (!isExist) {
return 0;
}
if (isDir) {//文件夹
NSArray *arr = [fileManaget contentsOfDirectoryAtPath:path error:nil];
int sum = 0;
for (NSString *fileName in arr) {//文件(夹)
NSString *fullPath = [NSString stringWithFormat:@"%@/%@",path,fileName];
sum += [self codeLinesWithPath:fullPath];
}
return sum;
}else{//文件
/*
NSString *extension = [[path pathExtension] lowercaseString];
if (!([extension isEqualToString:@"h"] || [extension isEqualToString:@"m"] || [extension isEqualToString:@"mm"])) {
return 0;
}
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [content componentsSeparatedByString:@"\n"];
return arr.count;
*/
NSString *extension = [[path pathExtension] lowercaseString];
if ([extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"] || [extension isEqualToString:@"gif"]) {
NSString *imageName = path.lastPathComponent;
NSArray *imgArr = [imageName componentsSeparatedByString:@"@"];
imageName = imgArr.firstObject;
imgArr = [imageName componentsSeparatedByString:@"."];
imageName = imgArr.firstObject;
self.isPicUsed = NO;
[self isImageUsedWithImageName:[NSString stringWithFormat:@"\"%@\"",imageName] path:@"Users/lixiang/Desktop/CrazyTeacher/Customer/CrazyTeacherApp"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSLog(@"lastPathComponent:%@,imageName:%@,size:%.1fkb,isUsed:%d",[path stringByReplacingOccurrencesOfString:@"Users/lixiang/Desktop/CrazyTeacher/Customer/CrazyTeacherApp" withString:@""],imageName,data.length/1024.0,self.isPicUsed);
return 1;
} else {
return 0;
}
}
return 0;
}
- (void)isImageUsedWithImageName:(NSString *)imageName path:(NSString *)path{
if (self.isPicUsed) {
return;
}
NSFileManager *fileManaget = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isExist = [fileManaget fileExistsAtPath:path isDirectory:&isDir];
if (!isExist) {
}
if (isDir) {//文件夹
NSArray *arr = [fileManaget contentsOfDirectoryAtPath:path error:nil];
for (NSString *fileName in arr) {//文件(夹)
NSString *fullPath = [NSString stringWithFormat:@"%@/%@",path,fileName];
[self isImageUsedWithImageName:imageName path:fullPath];
}
}else{//文件
NSString *extension = [[path pathExtension] lowercaseString];
if ([extension isEqualToString:@"h"] || [extension isEqualToString:@"m"] || [extension isEqualToString:@"mm"] || [extension isEqualToString:@"xib"] || [extension isEqualToString:@"storyBoard"]) {
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if ([content containsString:imageName]) {
self.isPicUsed = YES;
}
}
}
}
3.调用
NSInteger lineCount = [self codeLinesWithPath:@"Users/lixiang/Desktop/CrazyTeacher/Customer/CrazyTeacherApp"];
标签:
原文地址:http://www.cnblogs.com/xiangli/p/5259364.html