25

Since XCode 4, there is now a Code Snippets section which offers the snippets via auto-complete when typing. I'd be very interested in snippets you all have stored in there. What snippets save you the most time (and why)?

Please only post actual snippets (meaning no snarky "don't need no stinkin' snippets", and no "i love snippets that do <XYZ>"), and only snippets that are short and sweet (i.e. no more than ~20 lines at the most...). If a snippet is not obviously useful, also explain why you think it is. ;)

Epaga
  • 38,231
  • 58
  • 157
  • 245
  • BTW, why can't i mark this question as community wiki? – Epaga Mar 11 '11 at 07:51
  • Community wiki as a concept has changed over time - you can no longer simply mark a question as community wiki (although you can still tag answers in this way). However, a moderator can still make this change if requested to do so and if the question is deemed valid. – John Parker Mar 11 '11 at 07:51
  • 4
    I'm going to mark this as CW, but please take care to manage this question. Flag for help if you need it. I'll re-visit it in a few days. If this starts collecting noise, we have no choice but to close it. – Tim Post Mar 11 '11 at 08:09
  • I've also made a minor edit to your question so that more protracted answers are invited. – Tim Post Mar 11 '11 at 08:17
  • just saw that. I put it back in :) – Epaga Mar 11 '11 at 08:19
  • Great question! I haven't seen Xcode 4 yet and I'm on iOS at the moment, but I'll try to get to this one soon! – Moshe Mar 11 '11 at 08:30
  • 2
    Well I suppose that people are not willing to give their secret formula while developing in `objective-c` – Robin Mar 15 '11 at 04:12
  • 1
    yeah so much for the "noise" :-/ – Epaga Mar 15 '11 at 13:30

9 Answers9

10

I don't if this counts but I always use this snippet whenever I add a UITableView in any of my view controllers.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                      reuseIdentifier:cellIdentifier];
            // Do something here......................
    }
    // Do something here too .........................
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return ;
}

its quite handy if you are not using a UITableViewController to show table contents.

Robin
  • 10,011
  • 5
  • 49
  • 75
9

Dispatch block on the current queue after given number of seconds:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, <#seconds#>*1e9),
    dispatch_get_current_queue(), <#block#>);
zoul
  • 102,279
  • 44
  • 260
  • 354
7

Here are my two comment snippets. I use them a lot.

Header comment:

// ----------------------------------------------------------------------------------------------------------------
# pragma mark -
# pragma mark <#comment#>
# pragma mark -
// ----------------------------------------------------------------------------------------------------------------

Sub comment:

// ----------------------------------------------------------------------------------------------------------------
//  <#comment#>
// ----------------------------------------------------------------------------------------------------------------
Obiwahn
  • 2,677
  • 2
  • 26
  • 36
6

I’m often adding private class interfaces with class extensions:

@interface <#ClassName#> ()
@end

This is to keep the public interface completely free from internal stuff, especially now that we can have purely synthesized properties (example gist).

Epaga
  • 38,231
  • 58
  • 157
  • 245
zoul
  • 102,279
  • 44
  • 260
  • 354
5

A couple of collections are here:

https://github.com/mneorr/snippie/tree/master/backup

and here:

https://github.com/jad/xcode-code-snippets

which you can stick in this folder:

~/Library/Developer/Xcode/UserData/CodeSnippets
caliss
  • 29
  • 1
  • 3
5

While debugging this snippet is really useful. It let you know Class Name, Function Name and you can add your comments also.

NSLog(@"%s [Line %d] %@ ", __PRETTY_FUNCTION__, __LINE__,<#comment#>);
Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
4

This is the Blog, that I have created for the same purpose...

http://ios-funda.blogspot.in/

Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
4

There does not seem to be a class category between the factory snippets:

@interface <#ClassName#> (<#CategoryName#>)
@end
zoul
  • 102,279
  • 44
  • 260
  • 354
0

I also have the standard view lifecycle methods in my snippets (which get used daily):

I use keyboard shortcut vwa for

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];


}

vdl for viewDidLoad etc.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160