Short and useful Objective-C snippets?

Since Xcode 4, there is now a code snippet section that offers snippets through autocomplete when typing. I would be very interested in the fragments that you all saved there. Which fragments keep you the most (and why)?

Please post only the actual snippets (which means that snarky β€œdon't need any sninkin snippets” and no β€œi love snippets that make <XYZ>"), and only snippets that are short and sweet (i.e. no more than ~ 20 lines maximum). If the snippet is clearly not useful, explain why you think so.;)

+24
objective-c xcode xcode4 code-snippets
Mar 11 2018-11-11T00:
source share
9 answers

I don't know if this is the case, but I always use this snippet when I add a UITableView in any of my 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 very convenient if you are not using the UITableViewController to display the contents of the table.

+10
Mar 11 '11 at 9:12
source share
β€” -

Sending a block in the current queue after the specified number of seconds:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, <#seconds#>*1e9), dispatch_get_current_queue(), <#block#>); 
+9
Mar 17 2018-11-11T00:
source share

Here are my two comments. I use a lot of them.

Headline Comment:

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

Subcomponent:

 // ---------------------------------------------------------------------------------------------------------------- // <#comment#> // ---------------------------------------------------------------------------------------------------------------- 
+7
Apr 11 2018-11-11T00:
source share

Im often adds private class interfaces with class extensions:

 @interface <#ClassName#> () @end 

This means that the public interface is completely free of internal things, especially now that we can have purely synthesized properties ( gist example ).

+6
Mar 11 2018-11-11T00:
source share

Several collections are here:

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

and here:

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

which you can paste into this folder:

 ~/Library/Developer/Xcode/UserData/CodeSnippets 
+5
06 Sep '12 at 11:27
source share

While debugging this snippet is really useful. It allows you to find out the name of the class, the name of the function and you can also add your comments.

 NSLog(@"%s [Line %d] %@ ", __PRETTY_FUNCTION__, __LINE__,<#comment#>); 
+5
Feb 14 '13 at 13:13
source share

There seems to be no class category between factory fragments:

 @interface <#ClassName#> (<#CategoryName#>) @end 
+4
Mar 11 '11 at 9:20
source share

This is a blog that I created for the same purpose ...

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

+4
05 Oct
source share

I also have standard presentation lifecycle methods in my snippets (which are used daily):

I use v w a shortcut for

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

v d l for viewDidLoad etc.

0
Mar 04 '14 at 1:43 on
source share



All Articles