The code is as follows, the code has been annotated, if you need to test your own code to the text of the "need to test the address of the Code" to its own address can be:
1 //2 //main.m3 //1114 //5 //Created by Sun Zeming on 15/9/8.6 //Copyright (c) 2015 Sun Zeming. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten OneNsuinteger ZMSUMDM (NSString *path) A { - -Nsfilemanager *zmmanger =[Nsfilemanager Defaultmanager]; the -BOOL Zmyes =NO; - //determines whether the file path exists, if there is yn return yes, if no yn returns no, - //The &yes value is whether the path refers to a file or a folder, if the folder value is yes, if the file return value is no +BOOL Zmyn = [Zmmanger fileexistsatpath:path isdirectory:&Zmyes]; - + //If this path does not exist, the end program A if(! Zmyn)return 0; at - //if this path exists and is determined to be a folder - if(zmyes) { - //This is a folder - //returns all contents in a folder, returns an array of values -Nsarray *array =[Zmmanger Contentsofdirectoryatpath:path error:nil]; in //NSLog (@ "%@", array); - to intZmcount =0; + for(NSString *filenameinchArray) { - the //get the full path . *NSString *fullpath = [NSString stringWithFormat:@"%@/%@", Path,filename]; $ //NSLog (@ "%@", FullPath);Panax Notoginseng -Zmcount + =ZMSUMDM (fullPath); the + } ANSLog (@"Count:%zd", zmcount); the returnZmcount; + - } $ Else{ $ intZS =0; - - //This is a file the - //Determine the number of rows for a file with a. h/.m/.c extensionWuyi the //get the file extension that the path refers to and convert it to lowercase (lowercasestring This method converts the letter to lowercase) -NSString *ZMKZM =[[Path pathextension]lowercasestring]; Wu - if(! [Zmkzm isequaltostring:@"m"]&&! [Zmkzm isequaltostring:@"h"]&&! [Zmkzm isequaltostring:@"C"])return 0; About $ - //load the contents of the file (convert all the contents of the file into a string) -NSString *dress =[NSString Stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; - //separates the string with "\ n" as a delimiter, separating the array ANsarray *array = [Dress componentsseparatedbystring:@"\ n"]; + the //Replace the string @ "/users/sunzeming/desktop/" in path with an empty -Nsrange Range =[path rangeofstring:@"/users/sunzeming/desktop/"]; $NSString *thpath =[path stringbyreplacingcharactersinrange:range withstring:@""]; theNSLog (@"file path:%@, the number of lines of code is:%zd", thpath,array.count); the //remove the number of lines in the code that are blank the intzmsjdms=0; the for(NSString *arrinchArray) { -NSLog (@"%@", arr); in if([arr isequaltostring:@""]) { the theZmsjdms + =1; AboutZS =Zmsjdms; the the } the } + returnarray.count-Zs; - the }Bayi the } the - - intMain () { the theNsuinteger count = ZMSUMDM (@"The address where the code needs to be tested "); the theNSLog (@"The number of lines of code for the file you are looking for is:%zd", count); - the return 0; the the}
The implementation of the Count code lines (excluding blank lines)