Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short and useful Objective-C snippets?

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. ;)


2 Answers

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.

like image 79
3 revsRobin Avatar answered Sep 13 '25 09:09

3 revsRobin


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#>);
like image 31
zoul Avatar answered Sep 13 '25 11:09

zoul