Use some of the classes in the foundation:
Nsmanager
NSString
Nsarray
main.m//10-Calculates the number of lines of code////Created by xxx on 15/9/16.//Copyright (c) 2015 Itcast. All rights reserved.//#import <foundation/foundation.h>int countlineoffile (NSString * path) {//Get File Manager nsfile Manager * Manager = [Nsfilemanager Defaultmanager]; Determine if path is not a computable file path BOOL isfile = NO; BOOL exist = [Manager Fileexistsatpath:path isdirectory:&isfile]; if (!exist) {//path file path does not exist with return 0; }//If path exists if (isfile) {//path is folder//get all contents under current folder: folder and file Nsarray * Filearray = [Manager cont Entsofdirectoryatpath:path Error:nil]; int count = 0; View all files and folders for (NSString * file in Filearray) {count + = Countlineoffile ([NSString stringWithFormat : @ "%@/%@", Path,file]); } return count; } else{//Get file extension nsstring * extension = [path pathextension]; if (! ( [Extension isequaltostring:@ "h"] | | [Extension isequaltostring:@ "M"] || [Extension isequaltostring:@ "C"] | | [Extension isequaltostring:@ "CPP"]) return 0; Get all contents of file path to path file NSString * Fileconten = [NSString stringwithcontentsoffile:path Encoding:nsutf8stringencodin G Error:nil]; A string is divided into multiple strings with ' \ n ', that is, each line as a string Nsarray * array = [Fileconten componentsseparatedbystring:@ "\ n"]; for (NSString * str in array)//NSLog (@ "%@", str); return (int) Array.count; }}int Main (int argc, const char * argv[]) {//count the number of lines of code in a file or folder, including empty lines int count = Countlineoffile (@ "/users/qujie/ DOCUMENTS/OC program/10-Calculate the number of lines of code "); NSLog (@ "Count =%d", count); NSString * str = @ "123-456-789";//Nsarray * array = [str componentsseparatedbystring:@ "-"];//NSLog (@ "%@", arr AY); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
17th: Calculate Code line number practice code