知道你在一个 Xcode 项目中有多少行代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7224569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:49:40  来源:igfitidea点击:

Know how many lines of code you have in an Xcode project?

objective-cxcodecocoa-touch

提问by LA12

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?

我有一个相当大的项目,最终我完成了它。我只是想知道我的项目中总共有多少行代码。我使用的是 Xcode 3。那么你真的能找出编译了多少行代码吗?

回答by Esqarrouth

Open up Terminal.app, go into your project's root directory, and run this command:

打开 Terminal.app,进入你项目的根目录,然后运行这个命令:

For Swift only:

仅适用于 Swift:

find . \( -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C only:

仅适用于 Obj-C:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) -exec wc -l '{}' \+

For Obj-C + Swift:

对于 Obj-C + Swift:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C + Swift + C + C++:

对于 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

终端快速提示:
ls:列出目录内容
cd:更改目录
按 T​​ab 键自动完成
记住在空格前放置“\”反斜杠
我建议从主项目向下移动一个文件夹,这样您就可以摆脱框架中的代码计数

回答by Adam Rosenfield

Open up Terminal.app, cdinto your project's root directory, and run this command:

打开 Terminal.app,cd进入项目的根目录,然后运行以下命令:

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 \*.extclauses.

如果您还想包括在计数中的其他文件类型,请添加更多-o \*.ext子句。

回答by Cyril

You can use sloccountor cloc to do this, they are both compatible with Objective-C code.

您可以使用sloccountcloc来执行此操作,它们都与 Objective-C 代码兼容。

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.

我推荐使用 sloccount,如果你也使用 Jenkins,你可以获得不错的 HTML 报告。HTML 报告将使您能够深入了解不同的目录和文件。

This is a command line for just having an overview of your code, if you are in the root dir of your Xcode project:

如果您位于 Xcode 项目的根目录中,则这是一个仅用于概述代码的命令行:



    sloccount --duplicates --wide YOUR-TARGET-NAME

And if you want to generate a report to use in Jenkins, just add the --detailsflag:

如果你想生成一个报告以在 Jenkins 中使用,只需添加--details标志:



    sloccount --duplicates --wide --details YOUR-TARGET-NAME > build/sloccount.sc

and install the Jenkins plugin for sloccount via Jenkins UI.

并通过 Jenkins UI 为 sloccount 安装 Jenkins 插件。

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.

您将能够在这篇博客文章中看到 Jenkins 中此类报告的示例(免责声明:我是作者):http: //blog.octo.com/en/jenkins-quality-dashboard-ios-development/#step1- 1.

回答by bryanmac

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.

一种方法是将副本加载到 Xcode 中并使用“Xcode 4 项目分析器”。在 Apple Mac App Store 中搜索“Xcode”。我没有使用过这个程序,但我昨天在 Mac App Store 中搜索 Xcode 相关应用程序时碰巧看到了它。

Hope that helps.

希望有帮助。

回答by Thellimist

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.

查找文件行数的 bash 脚本WITHOUT COMMENTS AND EMPTY LINES。该脚本不计算以//. 将脚本复制到您的项目文件夹并通过sh scriptname.sh.

For swift change \( -iname \*.m -o -iname \*.mm -o -iname \*.h \)to \( -iname \*.swift \)

快速更改\( -iname \*.m -o -iname \*.mm -o -iname \*.h \)\( -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

回答by Sedate Alien

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 cloctool works well with Objective-C code.

我不确定是否有任何直接插入 Xcode 的工具(当 4.1 在 Lion 上免费提供时,为什么您仍在使用 Xcode 3?),但我发现命令行cloc工具与 Objective-C 代码配合得很好。

回答by Eradicatore

A really nice unix command is xargs. See the "pipe to xargs below". For example:

一个非常好的 unix 命令是 xargs。请参阅下面的“管道到 xargs”。例如:

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

奇怪的是,我必须弄清楚为什么这个答案对我来说比@Esqarrouth 的答案低一点