How to find out how many lines of code there are in an Xcode project?

Viewed 101264

Is there a way to determine how many lines of code an Xcode project contains? I promise not to use such information for managerial measurement or employee benchmarking purposes. ;)

14 Answers

In terminal, change into the project directory and run:

find . -type f -print0 | xargs -0 cat | wc -l

If you want only certain file types, try something like

find . -type f -name \*.[ch]* -print0 | xargs -0 cat | wc -l

Check out Xcode Statistician, it does exactly what you want. It also provides other interesting statistics so is worth a run for fun now and then.

Note that it will not look inside real folders, though it will look in groups. Odds are you aren't using real folders so it'll work great. If you are using folders then you just have to do the count in each folder and add them together.

Note: As of June, 2012, it seems this does not work properly with the latest versions of Xcode.

Steps to implement CLOC library in Mac as below:

  1. Open Terminal.
  2. Install Homebrew by copying and pasting the below command in the Terminal (including double quotes).

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Enter system password if asked.

You will see the terminal screen as below.

Installing Homebrew

System will popup so many permissions, allow all the permissions

If everything goes fine, you will see terminal screen as below,

Homebrew installation successful

  1. Now its time to install CLOC using below command.

brew install cloc

  1. Navigate to the project directory and run either of the following commands.

cloc . --exclude-dir=Pods (to exclude pod files)

cloc . (including pod files)

If everything goes fine, it will display the number of lines of code as below,

CLOC Result

A quick & easy way:

Use a regex search (Find Navigator, choose Find > Regular Expression).

.\n

Works conveniently with Xcode search scopes and you can easily customize it to whatever type of line you'd like to count ;).

You can install SLOCCount through MacPorts. Or, more crudely, you can use wc -l.

Related