Xcode:调整自动生成的大括号的缩进?

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

Xcode: Adjusting indentation of auto-generated braces?

xcodeindentation

提问by PlagueHammer

Code auto-generated by Xcode seems to have the opening brace on the same line by default:

默认情况下,Xcode 自动生成的代码似乎在同一行上有大括号:

@interface Controller : NSObject {

}

I'd like the opening brace on a line of its own, like this:

我想要单独的一行开始大括号,如下所示:

@interface Controller : NSObject 
{

}

This applies in general to any method / class auto-generated by Xcode. In Xcode preferences I have "Indent solo { by" set to 0: alt text

这通常适用于 Xcode 自动生成的任何方法/类。在 Xcode 首选项中,我将“Indent solo { by”设置为 0: 替代文字

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Laughing_Hyman

For Xcode 3.x, you can use the following: If you open up a terminal session and enter
defaults write com.apple.Xcode XCCodeSenseFormattingOptions '{ "BlockSeparator" = "\n" ; }'
it will start new blocks of code on a new line. Note that you will have to restart XCode if you have it opened in order for the new defaults to be read and used.

对于 Xcode 3.x,您可以使用以下内容: 如果您打开一个终端会话并输入
defaults write com.apple.Xcode XCCodeSenseFormattingOptions '{ "BlockSeparator" = "\n" ; }'
它将在新行上开始新的代码块。请注意,如果您打开了 XCode,则必须重新启动它,以便读取和使用新的默认值。

For Xcode 4.x to 6.x (current) I suggest you use Snippet Editas a handy tool for editing the snippets that define the Xcode autocompletion. At the current time it's free and works great.

对于 Xcode 4.x 到 6.x(当前),我建议您使用Snippet Edit作为一个方便的工具来编辑定义 Xcode 自动完成的代码段。目前它是免费的并且运行良好。

回答by Peter Hosey

The New Projectand New Filecommands don't really generate any code; they fill in templates.

新建项目新建文件命令并不真正产生任何代码; 他们填写模板

You'll need to create your own templates (probably based on Apple's) with the changes you want.

您需要使用所需的更改创建自己的模板(可能基于 Apple 的)。

In Xcode 3.1, the stock templates are in /Developer/Library/Xcode. You'll put your modified copies in ~/Library/ApplicationSupport/Developer/Shared/Xcode. Pay attention to the subfolders of those folders.

Xcode 3.1 中,库存模板在/Developer/Library/Xcode. 您会将修改后的副本放入~/Library/ApplicationSupport/Developer/Shared/Xcode. 注意这些文件夹的子文件夹。

回答by Sakari

This is something I would like to see too, I don't like the "new school" way of curly braces on the same line as functions or loop control.

这也是我想看到的,我不喜欢花括号与函数或循环控制在同一行的“新学校”方式。

When editing and shuffling lines of code around, it's easier to keep everything relevant in one line. Curly braces should only define scope, they're not part of the function definition or program execution (like in if, for etc..).

在编辑和改组代码行时,将所有相关内容放在一行中会更容易。花括号应该只定义范围,它们不是函数定义或程序执行的一部分(如 if、for 等)。

Modifying all the templates is not really a good solution, but thanks for the info anyway.

修改所有模板并不是一个好的解决方案,但还是感谢您提供的信息。

回答by Mike Shields

Read this: XCCodeSenseFormattingOptions

阅读本文:XCCodeSenseFormattingOptions

This document describes all the formatting options that auto-complete will use for brace and argument style. Here are mine:

本文档描述了自动完成将用于大括号和参数样式的所有格式选项。这是我的:

    XCCodeSenseFormattingOptions =     {
    BlockSeparator = "\n";
    PreMethodDeclSpacing = "";
};

回答by gavin

If you want to have your preferred style in all situations, you need a combination of Laughing_Hyman's answer and Peter Hosey's answer. In addition to what Peter Hosey said, you may also like to know that the Cocoa Touch file templates are in the following location. You can copy that directory to your custom templates too:

如果你想在所有情况下都有你喜欢的风格,你需要结合 Laughing_Hyman 的回答和 Peter Hosey 的回答。除了 Peter Hosey 所说的,您可能还想知道 Cocoa Touch 文件模板位于以下位置。您也可以将该目录复制到您的自定义模板:

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Classes/

The Xcode User Defaults give you your style when typing out code by hand, the file templates provide it when auto-generating a new class etc.

Xcode User Defaults 为您提供手动键入代码时的风格,文件模板在自动生成新类时提供它等。

回答by OlDor

I believe that "defaults write com.apple.Xcode" doesn't work on the latest versions of Xcode (7.x)

我相信“默认写入 com.apple.Xcode”不适用于最新版本的 Xcode (7.x)

The workaround is to edit the default Code Snippets.

解决方法是编辑默认代码片段。

However, Xcode doesn't allow to modify the default Code Snippets.

但是,Xcode 不允许修改默认代码片段。

In order to modify them, you can download a "Snippet Edit" program from here

为了修改它们,您可以从这里下载“片段编辑”程序