Short and useful Objective-C snippets?

前端 未结 9 1624
逝去的感伤
逝去的感伤 2021-01-30 07:27

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. Wh

相关标签:
9条回答
  • 2021-01-30 07:49

    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.

    0 讨论(0)
  • 2021-01-30 07:54

    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#>);
    
    0 讨论(0)
  • 2021-01-30 07:54

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

    Header comment:

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

    Sub comment:

    // ----------------------------------------------------------------------------------------------------------------
    //  <#comment#>
    // ----------------------------------------------------------------------------------------------------------------
    
    0 讨论(0)
  • 2021-01-30 07:56

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

    @interface <#ClassName#> (<#CategoryName#>)
    @end
    
    0 讨论(0)
  • 2021-01-30 07:58

    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
    
    0 讨论(0)
  • 2021-01-30 08:01

    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.

    0 讨论(0)
提交回复
热议问题