Every day there are new requirements, especially those business code, are repetitive labor, writing is inexplicable irritability. But there was no way. I had to get the job done. I recently recommended a hands-free arcane code block to my colleagues so that I could spare time for other things.

Use Code Snippets

This entry is different depending on the Xcode version. Currently, the Xcode12 version I’m using is in the upper right corner of the Xcode interface. Clicking the + sign will bring up the following interface. In fact, we found that the system already had a lot of code blocks, so you can browse and see what’s there, and you can type in Snippets and search for what you want to use, and it’s in OC and Swift

For example, if you find a code block called “@interface-category”, if you type “@interface-category” in the class, it will automatically generate the code, so that you don’t have to write the code for this function repeatedly.

  • The input is associative, and the code block is marked with {}
  • You can find a block of code you want to use and drag it over to use it

Custom code blocks

After all, the code block written by the system can not meet the needs of development, in fact, you can add some suitable for their own code block, but pay attention to the name and the system name is distinguished, so it is easy to knock customized. The method is also very simple, select formatting code, right click

When you click on the Create Code Snippet, the page appears with the name of the Code block, the summary, and the short name of the Code block. This abbreviation is we type code, put the abbreviation into the system will prompt the corresponding code block

So our custom code block is set up. For example, the code block I set is called xLabel for short. When typing XL, the system automatically prompts for the code block to appear. Double click again, the code is written automatically.

Custom code block in ~ / Library/Developer/Xcode/UserData/CodeSnippets, can go in and see

Common code blocks

Normally in development, we have a lot of scenarios that are often typed, such as:

  • Attribute definition, such as declaring an attribute
@property (nonatomic, strong) <type> <name>;

@property (nonatomic, weak) <type> <name>;

@property (nonatomic, copy) <type> <name>;

@property (nonatomic, assign) <type> <name>;
Copy the code
  • UI control writing, such as UIView, UILabel, UIButton, etc
    UIButton *button = [[UIButton alloc] init];
    button.backgroundColor = ;
    button.titleLabel.font = [UIFont systemFontOfSize:];
    [button setTitle: forState:UIControlStateNormal];
    [button setImage: forState: UIControlStateNormal];
    [button setTitleColor: forState:UIControlStateNormal];
    [button addTarget:self action: @selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
Copy the code
  • Common methods, such as UITableView’s proxy method
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"" forIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ;
}
Copy the code
  • , etc.
__weak typeof(self) weakSelf = self;
Copy the code

As long as it is often used in the scene, business code can be defined as suitable for their own code blocks, do not feel the trouble to add, the so-called sharpening does not mischop wood workers. Overall use is relatively convenient and simple, this code block will not be due to Xcode upgrade and upgrade can be added safely.