directory

preface

While coding specifications are a very personal matter, a good code specification is a common understanding. The following is a combination of reference articles and individual coding habits.

Line width

Try to keep it within 100, as follows

Xcode->Preferences->Text Editing->Page guide at column

type

  • When using integer variables, use NSInteger NSUInteger instead of int and uint.
  • When using float, CGFloat is recommended

constant

advice

static NSString *const PLMessageCountChanageNotificationName = @"MessageCountChanageNotification"
static NSUInteger const PLRetryMaxCount = 5Copy the code

Don’t recommend

#define PLMessageCountChanageNotificationName @"MessageCountChanageNotification"
#define PLRetryMaxCount 5Copy the code

variable

  • Just follow the hump pattern
  • It is recommended to name instance variables starting with an underscore
@interface Foo()
 {
    NSString *_name
 }
 @endCopy the code

literals

Syntax sugar is supported for NSString, NSDictionary, NSArray, and NSNumber

advice

NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingStreetNumber = @10018;Copy the code

Don’t recommend

NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingStreetNumber = [NSNumber numberWithInteger:10018];Copy the code

The enumeration

advice

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    UIViewAnimationCurveEaseInOut,         // slow at beginning and end
    UIViewAnimationCurveEaseIn,            // slow at beginning
    UIViewAnimationCurveEaseOut,           // slow at end
    UIViewAnimationCurveLinear,
    };Copy the code
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    };Copy the code

Don’t recommend

enum UIViewAnimationCurve {
  	UIViewAnimationCurveEaseInOut,         // slow at beginning and end
    UIViewAnimationCurveEaseIn,            // slow at beginning
    UIViewAnimationCurveEaseOut,           // slow at end
    UIViewAnimationCurveLinear,
    };Copy the code

Control statements

About the if

advice

if(condition) {
  //...
  } else {
  //...
  }Copy the code

Don’t recommend

if(condition)
{
  //..
  }
else
{
  //..
  }Copy the code

About the switch

Parentheses are not required and are recommended for clarity.

switch(condition){
  case 1:
  {
    //one line or multi-line
  }
  break;
  default:
  break;
  }Copy the code

For an enumerated switch, default is not required

UIViewAnimationCurve curveType = UIViewAnimationCurveEaseInOut;
switch(curveType) {
  case UIViewAnimationCurveEaseInOut:
  {

  }
  break;
  case UIViewAnimationCurveEaseIn:
  {

  }
  break;
  case UIViewAnimationCurveEaseOut:
  {

  }
  break;
  case UIViewAnimationCurveLinear:
  {

  }
  break;
  }Copy the code

About the For loop

For collection types (array, set) recommended enumerateObjectsUsingBlock

NSArray *nameArray = @[@"Kita","jonas"];
[nameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //...
 }];Copy the code

The second option is for in if you don’t need an index

for(NSString *name in nameArray) {
  //...
  }Copy the code

attribute

  • Attribute modifiers are all specified, default to (atomic,assign) for scalar types and default to (atomic,strong) for object types. The permission modifier can not be readwrite by default. Generally, the attribute is readwrite. For read-only attributes, you need to specify readonly
  • For a class with variable types (nsstrings, NSArray, NSDictionary, NSSet) it is recommended to use the copy, because may reference variable object

advice

@property(nonatomic,copy) NSString *name;
@property(nonatomic,strong) UIView *containerView;Copy the code

Don’t recommend

@property(nonatomic,strong) NSString *name;
@property(nonatomic) UIView *containerView;Copy the code

methods

The method type (-/+) should be followed by a space

advice

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;Copy the code

Don’t recommend

-(void)setTitle:(nullable NSString *)title forState:(UIControlState)state;Copy the code

annotation

File comment

Comments are key to code readability, and the best code should be self-documenting.

Create templates using Xcode Snippets

Template Suggestions

/** **<# brief description of the contents of the file #> **<# author #> **<# Copyright information statement (e.g. Copyright 2008 Google Inc.) #> **<# License template if necessary. Choose an appropriate authorization template for your project (for example, Apache 2.0, BSD, LGPL, GPL). # > * /Copy the code

Other annotations (class annotations, method annotations, etc.)

Xcode8 recommends using the built-in annotation shortcut command+option+/

The vvdocumenter annotation plugin is recommended for the following versions of xcode8

Code structure organization

#pragma mark - Lifecycle
- (instancetype)init {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
- (void)dealloc {}
#pragma mark - Getter
- (NSString *)name;
#pragma mark - Setter
- (void)setName:(NSString *)name;
#pragma mark - Public Methods
- (void)publicMethod {}
#pragma mark - Private Methods
- (void)privateMethod {}
#pragma mark - Some Delegate
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {}
#pragma mark - IBActions
- (IBAction)submitData:(id)sender {}Copy the code

Nscimplies is used as an assertion

Use NSCAssert instead of Assert or NSAssert

This is because the NSAssert macro holds self internally, especially in blocks, which is extremely easy to cause a retain cycle.

Use the CGRect function

advice

CGRect frame = self.view.frame; CGFloat x = CGRectGetMinX(frame); CGFloat y = CGRectGetMinY(frame); CGFloat width = CGRectGetWidth(frame); CGFloat height = CGRectGetHeight(frame); CGRect frame = CGRectMake(0.0, 0.0, width, height)Copy the code

Don’t recommend

CGRect frame = self.view.frame;
CGFloat x = frame.origin.x;
CGFloat y = frame.origin.y;
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
CGRect frame = (CGRect){ .origin = CGPointZero, .size = frame.size };Copy the code

reference

Dl.dropboxusercontent.com/s/5utnlwhr1…

Github.com/raywenderli…

Useful – Google – styleguide. Readthedocs. IO/en/latest/g…