分配快捷键以在Xcode中添加代码模板 [英] Assigning shortcut keys for adding code templates in Xcode
问题描述
这可能是一个愚蠢的问题,但我已经开始学习Xcode了,今天我想到了一件事。
This might be a silly question, but I've started learning Xcode and one thing came to my mind today.
也就是说,有些代码会再次被重复使用整个项目再次。
That is, some codes are reused again and again throughout the project.
例如,出于调试目的,你可以把 NSLog(@错误!);
在许多不同的地方。
For example, for debugging purpose you can put NSLog(@"error!");
in many different places.
是否可以指定一些快捷键来在 Xcode中添加这些模板代码
,这样每当你需要添加一个模板时,你可以点击相应的快捷键吗?
Is it possible at all to assign some shortcut keys for adding these "template" codes in Xcode
, so that whenever you need to add a template, you can just hit the corresponding shortcut key?
我试过google它,但我没有很幸运。
I have tried to google it, but I didn't get lucky.
这可能是一个微不足道的功能,甚至是一个不必要的功能,但是如果可以只按一个键组合而不是输入一行代码,我认为这有助于提高编码速度。
This might be a trivial function or even an unnecessary one, but if it is possible to hit just one combination of keys instead of typing a line of code, I think it will help improving the speed of coding.
任何想法?如果不在 Xcode
中,是否有其他IDE提供此功能?
Any ideas? If not in Xcode
, does any other IDE provide this function?
谢谢。
推荐答案
是的,你可以!
设置它有点不直观,但这是你如何做到的。
Setting it up is slightly non-intuitive, but here is how you can do it.
- 输入要模板化的代码。
- 在右侧栏中,选择水平工具栏中的代码片段库选项位于栏的中间(具有
{}
图标)。 - 选择代码并拖动它到代码片段库。
- Type the code you want to templatize.
- In the right-side bar, select the Code Snippet Library option in the horizontal toolbar halfway down the bar (which has the
{}
icon). - Select your code and drag it to the Code Snippet Library.
请参阅上图。您想要替换的任何内容,请附在<#whatever#>
中。
Refer to the image above. Anything you want to be replacable, enclose in <# whatever #>
.
在上面这个屏幕截图中,我将快捷方式设置为 d d d ,可替换的文本是<#protocol#>
。
In this screen shot above, I have set the shortcut to ddd, and the replacable text is <# protocol #>
.
现在,当我输入快捷方式时,我明白了:
Now, when I type the shortcut, I see:
点击标签完成:
您可以使它们任意复杂。例如,这是一个多行代码片段,用于在调试 UIView
层次结构或布局问题时勾勒出一个视图:
You can make them arbitrarily complex. For example, here is a multi-line snippet that I use to outline a view when debugging UIView
hierarchy or layout issue:
这篇关于分配快捷键以在Xcode中添加代码模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!