How to Count Lines of Code in an Xcode Project

How to Count Lines of Code in an Xcode Project

Written by

in

Quick answer: Xcode has no built-in line counter. Use CLOC (brew install cloc) for a detailed breakdown by language, or run find . -name "*.swift" -o -name "*.m" -o -name "*.mm" -o -name "*.cpp" | xargs wc -l in Terminal for a quick total.

Why this happens

Xcode does not include a “lines of code” counter in its interface. Apple’s IDE focuses on build-time metrics like compile warnings, test coverage, and binary size rather than source-code volume. When you need a line count — for project estimation, code-review sizing, or simple curiosity — you must reach for external tools or the command line.

An Xcode project typically mixes several languages: Swift, Objective-C, Objective-C++, C, C++, and sometimes generated code from Core Data models or Protobuf definitions. A reliable count must handle all relevant extensions and exclude build artifacts, dependency directories (Pods, Carthage, .build), and the .xcodeproj package itself.

How to fix it

1. Use CLOC — the most thorough option

CLOC (Count Lines of Code) is the community standard. It recognizes dozens of languages, separates blank lines, comments, and physical source lines, and produces a clean summary table.

Install (macOS with Homebrew):

brew install cloc

Run against your project root:

cloc --exclude-dir=Pods,.build,Carthage,DerivedData .

Typical output:

Language                     files          blank        comment           code
Swift                           42           1203            892           5412
Objective-C                     18            412            310           2105
C/C++ Header                     7             89             45            321
Markdown                         3             56              0            210
----------------------------------------------------------------------
SUM:                            70           1760           1247           8048

Useful flags:
– --by-file — per-file breakdown
– --csv / --json — machine-readable output for CI dashboards
– --exclude-lang=Markdown,JSON — skip non-source files
– --force-lang=Swift,*.metal — treat .metal files as Swift for counting

CLOC automatically skips .git, vendor folders, and binary files, so you rarely need complex exclusion rules.

2. Quick one-liner with find and wc

If you only need a rough total and don’t want to install anything, the classic shell pipeline works:

find . \( -name "*.swift" -o -name "*.m" -o -name "*.mm" -o -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) \
  -not -path "*/Pods/*" -not -path "*/.build/*" -not -path "*/Carthage/*" -not -path "*/DerivedData/*" \
  -print0 | xargs -0 wc -l

What it does:
1. find locates every source file with the listed extensions.
2. -not -path clauses prune common dependency directories.
3. -print0 | xargs -0 safely handles filenames with spaces.
4. wc -l counts newline characters in each file and prints a grand total.

Sample result:

   124 Sources/AppDelegate.swift
    87 Sources/Model/User.swift
  2315 Sources/Views/ContentView.swift
   ...
  8048 total

Add | awk '{sum+=$1} END {print sum}' to see only the final number.

3. Integrate into CI for trend tracking

Add a script phase to your scheme or a GitHub Actions step:

- name: Count lines of code
  run: |
    cloc --json --out=cloc.json --exclude-dir=Pods,.build,Carthage,DerivedData .
    echo "Total lines: $(jq '.SUM.code' cloc.json)"

Commit the JSON artifact or push the number to a time-series database (Datadog, Prometheus, or a simple Google Sheet) to visualize codebase growth over months.

4. Exclude generated code automatically

Generated files (Core Data NSManagedObject subclasses, SwiftProtobuf output, SwiftGen assets) inflate counts. Extend the CLOC exclusion list:

cloc --exclude-dir=Pods,.build,Carthage,DerivedData \
     --exclude-list-file=.clocignore .

Create .clocignore at the repo root:

*Generated*.swift
*+CoreData*.swift
*.pb.swift
SwiftGen/

CLOC reads one pattern per line and skips matching paths.

How to prevent it

  • Don’t use line count for performance reviews — it rewards verbosity, not clarity.
  • Pin CLOC version in CI (brew install cloc@1.98) so historical numbers stay comparable.
  • Document your exclusion list in the repo README so new contributors run the same command.
  • Run the count on clean clones — build artifacts in DerivedData can add thousands of phantom lines.

FAQ

Does CLOC count SwiftUI preview providers?

Yes. Any .swift file containing #Preview or PreviewProvider structs is counted as regular Swift code. If you want to exclude preview-only files, add a naming convention (e.g., *Previews.swift) and list it in .clocignore.

How do I count only my target’s sources, not test files?

Add --exclude-dir=*Tests,*UITests to the CLOC command, or adjust the find pattern to skip directories named Tests or ending in Tests.swift.

Can Xcode show this number in the Project Navigator?

No. Xcode 16 still has no built-in LOC display. Third-party plugins like “CodeMetrics” (via Alcatraz or manually loaded) can add a sidebar, but they rely on the same external tools under the hood and break on Xcode updates. Stick with CLOC or the terminal one-liner for stability.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.