preface

Every team should have a common code style and specification, which I believe is self-evident, I won’t go into details, we all know 😁. How to do this more efficiently, I’ll talk about how to better automate formatting your code.

The status quo

Most iOS developers are probably aware of the Clang Format plug-in for Xcode, which is an Xcode plug-in based on the Clang-format command line tool, but it is no longer available on Xcode9 because the plugin mechanism for Xcode9 has changed. Now you can use this XcodeClangFormat, click the link for the specific use, we go to see. One drawback is that it doesn’t automatically format when you save like the previous plugin (not to blame, Xcode’s new mechanism doesn’t allow that). However, using this plugin is not convenient enough. You have to manually select files or press shortcut keys to format the code, which is easy to forget and leads to the submission of non-standard code directly to the repository. Is there a way that I can type whatever I want, and then remind me when I submit it and automatically format it for me?

What to do

Space Commander, which uses Git Hooks to check code style before commit, only commit code that does not conform, list files that do not conform, and provide Shell scripts for automatic formatting. Let me show you how to use it.

  • 1 clone Space Commander

    git clone https://github.com/square/spacecommander.git
  • 2. Install Space Commander CD in your project root directory, execute setup-repo.sh (clone from the project, so full path), and add a hidden file to the project root directory. Point to the. Clang-format file in the Space Commander repository, which by default contains a set of code rules. If you want to use your own rules, you can either go to the Space Commander repository and change them, or replace them with a new.
  • Let’s try submitting code

    BasedOnStyle: Chromium

    IndentWidth: 4

    AlignConsecutiveAssignments: true

    AlignConsecutiveDeclarations: true

    ObjCSpaceAfterProperty: true

    PointerAlignment: Right

    BreakBeforeBraces: Attach

    These are some rules that I have customized. Please refer to clang-format for detailsthis.

    Ok, so let’s write some code
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, copy) NSString*    p;
@property(nonatomic, strong) UITextView *  textview;
@end

@implementation ViewController

-(void)formatTest:(NSString *)param{
if (param) {
 NSLog(@"sss");
    }
    int a=0;
       int b = 1;
    int c= 2;
    NSLog(@"%d%d%d",a,b,c);
}

-(void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidAppear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end 
Copy the code

This looks very non-standard, so let’s submit it first

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, copy) NSString *    p;
@property (nonatomic, strong) UITextView *textview;
@end

@implementation ViewController

- (void)formatTest:(NSString *)param {
    if (param) {
        NSLog(@"sss");
    }
    int a = 0;
    int b = 1;
    int c = 2;
    NSLog(@"%d%d%d", a, b, c);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Copy the code

Perfect! 😝

subsequent

If you use ZSH, modify ~/.zshrc. If you use bash, modify ~/.bash_profile.

/ / initializationalias clangformatsetup="/ your own path /spacecommander/setup-repo.sh"// Format the corresponding filealias clangformatfile="/ your own path /spacecommander/format-objc-file.sh"// Format all temporary filesalias clangformatfiles="/ your own path /spacecommander/format-objc-files.sh"// Format the entire warehousealias clangformatall="/ your own path /spacecommander/ format-objc-files-in-rebo.shCopy the code

If you want to know more about how to use it, go directly to the github home page of spacecommander.

conclusion

This is my first time to write an article on the nuggets (other places have not written much 😂), write bad, everyone forgive, many suggestions ha 😁.