I've got quite a big project and eventually I finished it. I'm just curious to know how many lines of code there are altogether in my project. I'm using Xcode 3. So can you actually find out how many lines of code have been compiled?
-
9It's not the size of the code that counts, but what you do with it. – GolezTrol Aug 28 '11 at 23:01
-
possible duplicate of [How to find out how many lines of code there are in an Xcode project?](http://stackoverflow.com/questions/2003534/how-to-find-out-how-many-lines-of-code-there-are-in-an-xcode-project) – aliasgar Feb 05 '15 at 13:34
7 Answers
Open up Terminal.app, go into your project's root directory, and run this command:
For Swift only:
find . \( -iname \*.swift \) -exec wc -l '{}' \+
For Obj-C only:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) -exec wc -l '{}' \+
For Obj-C + Swift:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -exec wc -l '{}' \+
For Obj-C + Swift + C + C++:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h -o -iname \*.hh -o -iname \*.hpp -o -iname \*.cpp -o -iname \*.swift \) -exec wc -l '{}' \+
Terminal quick tips:
ls: list directory contents
cd: change directory
Press tab to autocomplete
Remember to put "\" backslash before spaces
I suggest going one folder down from the main project so you get rid of code count from the frameworks

- 38,543
- 21
- 161
- 168
Open up Terminal.app, cd
into your project's root directory, and run this command:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h \) -exec wc -l '{}' \+
If you other file types you also want to include in your count, then add more -o \*.ext
clauses.

- 390,455
- 97
- 512
- 589
-
2
-
-
1i needed with out commented lines and blank lines , how can I get this ? – Vineesh TP Oct 29 '14 at 10:58
-
You can use sloccount or cloc to do this, they are both compatible with Objective-C code.
I recommend using sloccount, you can get a nice HTML report if you also use Jenkins. The HTML report will enable you to drill down to the different directories and files.
This is a command line for just having an overview of your code, if you are in the root dir of your Xcode project:
sloccount --duplicates --wide YOUR-TARGET-NAME
And if you want to generate a report to use in Jenkins, just add the --details
flag:
sloccount --duplicates --wide --details YOUR-TARGET-NAME > build/sloccount.sc
and install the Jenkins plugin for sloccount via Jenkins UI.
You will be able to see examples of such reports in Jenkins in this blog article (disclaimer: I am the author): http://blog.octo.com/en/jenkins-quality-dashboard-ios-development/#step1-1.

- 101
- 1
- 5
-
Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Aug 28 '12 at 13:47
-
@andrew-barber thanks, I have corrected this and improved my post with a command line example. – Cyril Sep 03 '12 at 09:41
One way is to load a copy into Xcode and use "Project Analyzer for Xcode 4". Search for "Xcode" in the Apple Mac App Store. I have not used this program but I happened to see it yesterday when I was searching for Xcode related apps in the Mac App Store.
Hope that helps.
-
I use a free app called "Xcode analyzer" for this since years. Its probably not as good as "Project Analyzer" but it can count the toc and hey, its free. – JustSid Sep 01 '11 at 19:45
A bash script which finds line count of files WITHOUT COMMENTS AND EMPTY LINES
. The script doesn't count comments which start with //
. Copy the script to your project folder and run by sh scriptname.sh
.
For swift change \( -iname \*.m -o -iname \*.mm -o -iname \*.h \)
to \( -iname \*.swift \)
# $excluded is a regex for paths to exclude from line counting
excluded="some_dir_name\|some_dir_name2\|lib\|thisfile"
countLines(){
# $total is the total lines of code counted
total=0
# -mindepth exclues the current directory (".")
for file in `find . -mindepth 1 \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) | grep -v "$excluded"`; do
# First sed: only count lines of code that are not commented with //
# Second sed: don't count blank lines
# $numLines is the lines of code
numLines=`cat $file | sed '/\/\//d' | sed '/^\s*$/d' | wc -l`
# To exclude only blank lines and count comment lines, uncomment this:
#numLines=`cat $file | sed '/^\s*$/d' | wc -l`
total=$(($total + $numLines))
echo " " $numLines $file
done
echo " " $total in total
}
countLines

- 3,757
- 5
- 31
- 49
I'm not sure about any tools that plug into Xcode directly (why are you still using Xcode 3 when 4.1 is freely available on Lion?), but I find that the command-line cloc
tool works well with Objective-C code.

- 10,999
- 6
- 38
- 59
A really nice unix command is xargs. See the "pipe to xargs below". For example:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) | xargs wc -l
Oddly, I'll have to figure out though why this answer comes out a tiny bit lower for me than the answer from @Esqarrouth

- 1,501
- 2
- 20
- 38