我可以将自己的代码片段添加到 xcode 4 中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5303217/
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
Can I add my own code snippets to xcode 4?
提问by Chris Devereux
It'd be really handy to have collection KVC accessor snippets in xcode, as they're a pain to do by hand. Has anyone who's been using 4 for a while worked out how to add new ones?
在 xcode 中收集 KVC 访问器片段会非常方便,因为手动完成它们很痛苦。有没有人已经使用 4 一段时间了,想知道如何添加新的?
(...we're allowed to talk about it now, right?)
(……我们现在可以谈论它了,对吧?)
回答by Kendall Helmstetter Gelner
Yes, it's quite easy - you just highlight text you want to use as a snippet and drag it into the snippets area (dragging selected text can be difficult, I find it works better if you drag from the left edge of the editor). It brings up a dialog box asking you to name it.
是的,这很容易 - 您只需突出显示要用作片段的文本并将其拖到片段区域(拖动选定的文本可能很困难,我发现如果从编辑器的左边缘拖动效果更好)。它会弹出一个对话框,要求您为其命名。
Also, you can have custom parameters in snippets - if you put the text <#paramName#>
anywhere in the code you are dragging, when you use the snippet it will come up as a replaceable parameter that you can tab between just like in the official Snippets.
此外,您可以在代码段中使用自定义参数 - 如果将文本<#paramName#>
放在要拖动的代码中的任何位置,当您使用代码段时,它将作为可替换的参数出现,您可以像在官方代码段中一样在它们之间进行切换。
Also make sure to set a completion prefix, which makes it shorter to activate the snippet as you are typing. The nice thing about using Snippets over other solutions like global system text expanders, is that snippets can also be limited to being valid in a certain area of code - so for example a snippet that filled out a string formatting line can be marked as valid only within a block, not outside a method. That way the completion prefix only hits in areas where it's valid to use.
还要确保设置一个完成前缀,这样可以缩短您在键入时激活代码段的时间。与全局系统文本扩展器等其他解决方案相比,使用 Snippets 的好处是,snippets 也可以限制在特定代码区域中有效 - 例如,填充字符串格式行的代码段可以标记为仅有效在块内,而不是在方法外。这样,完成前缀只在它可以使用的区域有效。
You can edit a snippet by clicking once on it, a box will come up with the snippet text and some other snippet settings you can edit.
您可以通过单击一次来编辑代码段,一个框将显示代码段文本和您可以编辑的其他一些代码段设置。
回答by Imran
A tip to help you drag the code every time, select text then click + hold (do not move mouse) until mouse pointer changes from "I" to pointer. You'll then be able to drag the selected text to the Code Snippet Library.
帮助您每次拖动代码的提示,选择文本然后单击 + 按住(不要移动鼠标)直到鼠标指针从“I”变为指针。然后您就可以将选定的文本拖到代码片段库中。
回答by dusty
Drag the highlighted code to the Snippet library. (It can be stubborn and not want to drag. Holding the mouse button down for a moment before dragging seems to help.)
将突出显示的代码拖到 Snippet 库中。(它可能很顽固,不想拖动。在拖动之前按住鼠标按钮片刻似乎有帮助。)
回答by Vladimir Prudnikov
You can use Snippetsto manage code snippets in Xcode. Check out this demo http://www.youtube.com/watch?v=il4kE4diy0k
您可以使用Snippets来管理 Xcode 中的代码片段。看看这个演示http://www.youtube.com/watch?v=il4kE4diy0k
回答by Moe9977
Simply select the code, the press ALT key and drag it into the code snippets library and rename it there, Make sure to press the ALT key, other than this you won'n be able to drag it.
只需选择代码,按 ALT 键并将其拖到代码片段库中并在那里重命名,确保按 ALT 键,除此之外您将无法拖动它。