Xcode Snippets 功能详解

http://nshipster.com/xcode-snippets/

iOS development all but requires the use of Xcode. To its credit, Xcode has improved pretty consistently over the last couple of years. Sure, it still has its… quirks, but hey—things could be much, much worse.

Working in an IDE may not be as cool as working in your favorite decades-old editor (or that other one), but you know what is cool? Autocompletion. Not to mention Build & AnalyzeBreakpoints, andInstruments.

This is all to say: if we’re resigned to use an IDE in our development workflow, we might as well make the most of it, right? So this week on NSHipster, we’re going to talk about one of the more powerful yet underused features of Xcode: Code Snippets.


From @interface declarations to if (!self) return nil; incantations, there is a lot of avoidable typing in Objective-C. Xcode snippets allow these common patterns and boilerplate code to be extracted for quick reuse.

Using Xcode Snippets

To see the available code snippets, show the Utilities panel, to the right of your editor. On the bottom half the Utilities panel, there will be a horizontal divider with 4 icons.

Utilities Divider

Click the { } icon to show the Code Snippets Library.

Utilities Panel

There are two ways to insert a snippet into your code:

You can drag and drop from the code snippets library into your editor:

Drag-and-Drop

…or for snippets that include a text completion shortcut, you can start typing that:

Text Completion Shortcut

To get a sense of what you can do with snippets, here’s an overview of the ones built-in to Xcode:

  • typedef declarations for enumstruct union, and blocks
  • C control flow statements like ififelse, and switch
  • C loops, such as forwhile, and dowhile
  • C inline block variable declaration
  • Objective-C declarations for @interface (including for class extensions and categories),@implementation@protocol
  • Objective-C boilerplate for KVO, including the relatively obscurekeyPathsForValuesAffecting<Key>, used for registering dependent keys
  • Objective-C boilerplate for Core Data fetches, property accessors, and property validation
  • Objective-C idioms for enumerating NSIndexSet
  • Objective-C incantation for initinitWithCoder: and initWithFrame: method implementations
  • Objective-C @try / @catch / @finally and @autorelease blocks
  • GCD idioms for dispatch_once and dispatch_after

Creating Xcode Snippets

Of course, what really makes snippets such a powerful feature is the ability to create your own.

The process of creating a snippet is actually pretty unintuitive and difficult to explain. It uses an obscure OS X system feature that allows users to create a “Text Clipping” by dragging and dropping selected text. Much easier to just show it in action:

Text Completion Shortcut

After being added to the code snippet library, a user-defined snippet can be edited by double-clicking its listing:

Text Completion Shortcut

Each snippet has the following fields:

  • Title - The name of the snippet (appears in text completion and in snippet library listing)
  • Summary - A brief description of what it does (appears only in snippet library listing)
  • Platform - Limits the snippet visibility for text completion to the specified platform. OS X, iOS, or both (“All”)
  • Language - Limits the snippet visibility for text completion to the specified language. Most commonly C, Objective-C, C++, or Objective-C++.
  • Completion Shortcut - The text completion shortcut. For commonly-used snippets, this should be relatively short. Xcode does not warn about conflicting / overlapping shortcuts, so make sure yours doesn’t overlap with an existing one.
  • Completion Scopes - Limits the snippet visibility for text completion to the specified scopes. For example, an if / else statement should only be auto-completed from within a method or function implementation. Any combination of the following:
    • All
    • Class Implementation
    • Class Interface Methods
    • Class Interface Variables
    • Code Expression
    • Function or Method
    • Preprocessor Directive
    • String or Comment
    • Top Level

Each Xcode snippet has a file representation in~/Library/Developer/Xcode/UserData/CodeSnippets/

Placeholder Tokens

Something you may have noticed in using other Xcode snippets are placeholder tokens:

Placeholder Token

In Xcode, placeholder tokens are delimited by <# and #>, with the placeholder text in the middle. Go ahead—try typing that into Xcode, and watch as the text between the octothorp tags magically transforms right in front of your eyes.

Include placeholder tags to add a dash of dynamism in your own snippets!

Third-Party Xcode Snippets

A list of generally useful code snippets can be found in this GitHub project (pull requests welcome!). If nothing else, this also serves as an example of what’s possible.


Programming isn’t about being an expert typist, so don’t make it any more difficult for yourself than it needs to be. If you find yourself groaning while typing some inane, rote-memorized code idiom, take a minute to create a snippet for it instead!

nsmutablehipster

Questions? Corrections? Issues and pull requests are always welcome — NSHipster is made better by readers like you.

Find status information for all articles on the status page.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值