如何将自定义代码片段添加到 XCode 4?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6784627/
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
How to add custom code snippets to XCode 4?
提问by Jay Imerman
If you show the Utilities pane on the far right of the XCode window, then select the "{ }" button, it shows code snippets that you can type the shortcut for, or drag and drop into your source code. How do you create your own?
如果您在 XCode 窗口的最右侧显示“实用工具”窗格,然后选择“{}”按钮,它会显示您可以键入快捷方式或拖放到源代码中的代码片段。你如何创建自己的?
回答by Jay Imerman
Although the documentation is very scant on this, I did find a blog post by Jason Brennanthat describes step-by-step.
尽管关于此的文档非常少,但我确实找到了Jason Brennan的一篇博客文章,其中描述了逐步的。
- Type in the code you want to create a snippet for.
- Highlight the code.
- Drag the highlighted code and drop it on the Snippet utility section.
- Click the snippet, a popup caption dialog will appear. Edit the title, description, and shortcut.
- 输入要为其创建代码段的代码。
- 突出显示代码。
- 将突出显示的代码拖放到 Snippet 实用程序部分。
- 单击片段,将出现一个弹出标题对话框。编辑标题、描述和快捷方式。
NOTE: You can use prompters denoted by <#prompterName#> that will give blue bubbles that you can tab to, and replace with "parameters" in your source. For example:
注意:您可以使用由 <#prompterName#> 表示的提示器,它会显示蓝色气泡,您可以使用 Tab 键切换到,并替换为源中的“参数”。例如:
NSDictionary *<#varibleName#> = [[NSBundle mainBundle] infoDictionary];
NSDictionary *<#variableName#> = [[NSBundle mainBundle] infoDictionary];
NOTE: Depending on the language of the source code file you are editing, your new snippet will default to be in that language. In this way, snippets are selectively used for Swift or Objective-C depending on your context.
注意:根据您正在编辑的源代码文件的语言,您的新代码段将默认使用该语言。通过这种方式,可以根据上下文有选择地将代码段用于 Swift 或 Objective-C。
NOTE: If you attempt to drag highlighted text, and it unselects and starts a new selection instead, try to start your drag action in the whitespace off to the right of your selected text.
注意:如果您尝试拖动突出显示的文本,并且它取消选择并开始新的选择,请尝试在所选文本右侧的空白处开始拖动操作。
Also, to delete your snippets, click to highlight in the snippet library, and press the "Delete" key.
此外,要删除您的代码段,请在代码段库中单击以突出显示,然后按“删除”键。
回答by FreeAsInBeer
Just highlight your snippet and drag it into the snippets box.
只需突出显示您的代码段并将其拖到代码段框中即可。

