Xcode 4 code snippets

Source: Internet
Author: User

Http://nearthespeedoflight.com/article/xcode_4_code_snippets

Perhaps my favorite feature of xcode 4 is the code snippets feature. It allows you to use common bits quickly in your code, instead of requiring you retype them over and over again.

For example, every objective-C class requires you to providedeallocMethod
To clean up any memory you might be hanging on to (of course this is not needed if your subclass hasn't done anything it needs to clean up, but generally you shoshould be releasing or freeing any memory you 've asked for in the class if appropriate ). one mistake
I 've often made is the following:

- (void)dealloc {    [someObject release];}

While this looks fine, the problem is I 've neglected to invoke the super's implementation of the method (this ought to generate a compiler error, because you compile-werror,
Right ?). To avoid errors like this, xcode provides a snippet which demonstrates the proper pattern, has a placeholder for your code, and is available by simply typing "dealloc" and accepting the completion suggestion. it generates the following code for you:

- (void)dealloc {    (deallocations) // (code) will appear as a blue bubble.                              // pressing return will accept the bubble as text    [super dealloc];}

Xcode provides a bunch of these code snippets, which you can find by opening the utilities view on the right of your window. near the bottom, you'll find the code snippets library, in addition to the file template library (which works similarly ). snippets can
Be broken down by platform (Mac OS X, IOS) or viewed all together. Look through the available snippets to see how they might save you from repetitive typing in your code.

The provided snippets are useful, but the cause for my adoration stems from being able to add my own snippets to the library. I 've found this to be incredibly useful.

  1. Find some code you 'd like to be a snippet and select it.

  2. Drag the selected code to the snippet library.

  3. Give it a title and Optionally a summary. These are used when browsing through the snippet library list.

  4. Choose a completion into cut to invoke this snippet. My preference is to use the same letter, repeated three times (ieddd)
    Because it's quick to type, and it's very unlikely to conflict with another completion symbol in my code. not only does it avoid direct conflicts, but it also lets me filter down the list of suggestions quickly, so I can accept the completion as quickly
    Possible.

  5. Chose a platform and language, and completion scope (defaults to the current scope) which lets you choose when the completion into cut will be available (It wouldn't make sense for your initim initializer method snippet to appear in an objective-c @ interface ).

  6. Finally, you may wish to edit the snippet code you 've added. The mini-editor is syntax highlighted for you, although unfortunately you can't invoke other snippet completion shortcuts here! Sometimes trying this causes xcode to have an aneurism.

  7. Bonus: If you 'd like your own "blue code Bubbles" to appear as part of your snippet, just type the following in your snippet:<#text
    in the bubble#>
    When the user invokes the shortcut for the snippet, the inserted text will contain in any blue code bubbles you 've been ded in the hash tags. they can be tabbed back and forth (that is, pressing tab will select the next bubble) and can
    Be accepted by pressing return. You'll see why these are useful in my examples.

Snippets I find useful

Retain/assign property: Completion shortcutsrrrAndaaaRespectively.

@property (nonatomic, retain) <#type#> *<#name#>;@property (nonatomic, assign) <#type#> <#name#>;

Synthesize for a custom Ivar name: Completion completed cutsss

@synthesize <#property#> = _<#propertyIvar#>;

Useful when you have a custom naming scheme for your instance variables (such_ivarOrmMemberVariable).

Class extension interface: shortcuteee

@interface <#class name#> ()@end

Pragma line and name: shortcutppp.
This cocould alternatively be on one line. Also, when naming your mark label, write out the symbol name in full and it will be cmd + doubleclickable, ieUITableViewDelegate
methods
InsteadTable view delegate methods

#pragma mark -#pragma mark <#Label#>

Define macro: shortcutddd.
Xcode will AutoComplete#defineFor you out of
Box, but I find it slower and it doesn't create tabbable bubbles for you for both arguments. I find adding this snippet to be much more efficient.

#define <#name#> <#substitution#>

While code reuse is an incredibly important factor, there are some bits you just can't help but write over and over again. The code snippet library can seriously reduce the hassystemic of this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.