xcode Objective-C 整洁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/170601/
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 18:24:35  来源:igfitidea点击:

Objective-C Tidy

objective-ccocoaxcode

提问by drewh

I have a somewhat messily-formatted Objective-C code base. Is there a way to have Xcode reformat an entire project to conform to a coding standard (i.e., properly indent, spaces v. tabs, etc.)? Are there other tools that might accomplish this?

我有一个格式有点乱的 Objective-C 代码库。有没有办法让 Xcode 重新格式化整个项目以符合编码标准(即正确缩进、空格与制表符等)?是否有其他工具可以实现这一点?

回答by dbr

Uncrustify: http://uncrustify.sourceforge.net/

去壳:http://uncrustify.sourceforge.net/

Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA

C、C++、C#、ObjectiveC、D、Java、Pawn 和 VALA 的源代码美化器

If you want something simpler, you could probably get some way by simply stripping out all the white-space/line-breaks, and adding a new line-break on ;{}, and manually re-indenting the code. It won't be anywhere near perfectly laid out code, and reindenting could be a pain on large code, but it will be consistent.

如果您想要更简单的东西,您可以通过简单地去除所有空格/换行符并在 上添加新的换行符;{}并手动重新缩进代码来获得一些方法。它不会接近完美布局的代码,并且重新缩进对大型代码来说可能是一种痛苦,但它会保持一致。

回答by Jens Ayton

A simple, but limited, solution is Edit->Format->Re-Indent in Xcode, which will apply your current indentation settings (Xcode->Preferences…->Indentation).

一个简单但有限的解决方案是 Xcode 中的 Edit->Format->Re-Indent,这将应用您当前的缩进设置(Xcode->Preferences...->Indentation)。

回答by Vinko Vrsalovic

According to this blog post, bcppworks with Objective-C.

根据这篇博客文章bcpp与 Objective-C 一起工作。

In addition, the tool indent might help you. It's aimed at plain C but has a gazillion options that could help. I don't know if it comes by default on OS X though.

此外,工具缩进可能会对您有所帮助。它针对普通的 C,但有无数的选择可以提供帮助。我不知道它是否在 OS X 上默认出现。

NAME
       indent - changes the appearance of a C program by inserting or deleting
       whitespace.

SYNOPSIS
       indent [options] [input-files]

       indent [options] [single-input-file] [-o output-file]

       indent --version

DESCRIPTION
       This man page is generated from the file indent.texinfo.  This is  Edia
       tion  of "The indent Manual", for Indent Version , last updated .

       The  indent  program  can  be used to make code easier to read.  It can
       also convert from one style of writing C to another.

       indent understands a substantial amount about the syntax of C,  but  it
       also attempts to cope with incomplete and misformed syntax.

回答by Nathan Kinsinger

From Xcode:

从Xcode:

1) Change the Indentation preferences to match what you want.

1)更改缩进首选项以匹配您想要的。

2) Select a file to work on and Select All (cmd-A)

2) 选择要处理的文件并全选 (cmd-A)

3) Shift Left (cmd-[) several times until all lines are at the left edge of the window.

3) Shift Left (cmd-[) 几次,直到所有行都位于窗口的左边缘。

4) Use Re-Indent Selection (from Edit->Format-> or from the right-click contextual menu)

4)使用重新缩进选择(从编辑->格式->或从右键单击上下文菜单)

Only works on one file at a time, not the whole project. Also only deals with indentation.

一次只能处理一个文件,而不是整个项目。也只处理缩进。

回答by rcw3

After tinkering with multiple external formatters and the weak internal xcode formatter, I finally settled with uncrustify. Uncrustify has fairly good Objective-C support, can easily be integrated with xcode as a user script, and provides a centralized formatter for pretty much all languages that xcode natively supports.

在修改了多个外部格式化程序和弱的内部 xcode 格式化程序之后,我终于解决了 uncrustify。Uncrustify 具有相当好的 Objective-C 支持,可以轻松地作为用户脚本与 xcode 集成,并为几乎所有 xcode 原生支持的语言提供集中式格式化程序。

The biggest hurdle with uncrustify is the daunting configuration file. My recommendation, take one of the supplied sample configs (ben2.cfg is very good), merge in the objc.cfg sample, and tweak as necessary.

uncrustify 的最大障碍是令人生畏的配置文件。我的建议是,采用提供的示例配置之一(ben2.cfg 非常好),合并到 objc.cfg 示例中,并根据需要进行调整。

回答by Dan Rosenstark

One way to go that uses uncrustify in a different context is

在不同的上下文中使用 uncrustify 的一种方法是

http://universalindent.sourceforge.net/

http://universalindent.sourceforge.net/

回答by Alex Gray

The sole feature of the "GTMXcodePlugin", aka Google Toolbox For Mac Xcode Plugin- does a great job of tidying objective-c code, by effectively, and safely trimming whitespaces.

“GTMXcodePlugin”(又名Google Toolbox For Mac Xcode 插件)的唯一功能是 通过有效且安全地修剪空格,在整理 Objective-c 代码方面做得非常出色。

The GTM Xcode 4 plugincurrently only adds a "Clean Up Whitespace" menu item to the end of the "Edit" menu to remove unnecessary end of line white space from text files. Hopefully we will add more features soon. It has only been tested against Xcode 4.2.

GTM Xcode 4 插件目前仅在“编辑”菜单的末尾添加了“清理空白”菜单项,以从文本文件中删除不必要的行尾空白。希望我们很快会添加更多功能。它仅针对 Xcode 4.2 进行了测试。

回答by Clay Bridges

The newest, bestest way of doing this, as of this writing, is SpaceCommander. It's based on top of clang, and has lots of other neat features, and is maintained and active as of this writing.

在撰写本文时,最新、最好的方法是SpaceCommander。它基于 clang 之上,并具有许多其他简洁的功能,并且在撰写本文时一直保持活跃。