#import<Foundation/Foundation.h>intCodelinecount (NSString *path) { //1. Get the File ManagerNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //determine if path is a folder or a file pathBOOL dir = NO;//whether the tag is a folder//whether this path existsBOOL exist = [Mgr Fileexistsatpath:path isdirectory:&dir]; //3. If there is no if(!exist)return 0; //4 path exists if(dir) {//NSLog (@ "is a folder"); //Contains all the contents (folders, files) under the current folderNsarray *array =[Mgr Contentsofdirectoryatpath:path Error:nil]; intCount =0; //iterate through all the file names in the array for(NSString *filenameinchArray) { //full path to sub-file (clip)NSString *fullpath = [NSString stringWithFormat:@"%@/%@", Path,filename]; //Loop CallCount + =Codelinecount (FullPath); } returncount; } Else { //to determine the file name extensionNSString *extension =[[Path pathextension] lowercasestring]; if(! ([Extension isequaltostring:@"h"] || [Extension isequaltostring:@"m"] || [Extension isequaltostring:@"C"]) ){ //the file extension is not H, and is not M, and is not a C return 0; } //file//1. Loading file contentsNSString *content =[NSString Stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; //2. Separate the contents of the file into each row//create a character array by \ n DelimitedNsarray *array = [content componentsseparatedbystring:@"\ n"]; NSString*STR = [path stringbyreplacingoccurrencesofstring:@"/users/laouhn/documents/project"Withstring:@""]; NSLog (@"%@-%ld", Str,array.count); return(int) Array.count; }}intMainintargcConst Char*argv[]) { intCount = Codelinecount (@"/users/laouhn/documents/project"); NSLog (@"%d", Count); return 0;}//void Test ()//{//nsstring *str = @ "Jack\nrose\njim\njake";// //write something to the file//[str writetofile:@ "/users/laouhn/desktop/crayons.txt" Atomically:yes encoding:nsutf8stringencoding Error:nil]; // //Delimited by character//nsarray *array = [str componentsseparatedbystring:@ "\ n"];// // //}//
The number of lines in iOS that read all the text in a folder