Skip to content

Latest commit

 

History

History
82 lines (54 loc) · 2.91 KB

编码规范.md

File metadata and controls

82 lines (54 loc) · 2.91 KB

编码规范

  • 项目结构

目录结构

  • 布局中,不管是颜色,还是布局,不能有数字常量,必须都用#define定义,放在宏中。

  • .h文件中,需要每个方法等写简明注释。

  • .m文件中,每个方法需要用VVDocument进行详细注释。

  • 一些逻辑复杂或者很重要的逻辑,需要写行内注释。

  • delegate, dataSource等需要在.m中用#pragma mark - 进行分割。中间至少3行空白。

  • 小型图标可使用imageNamed生成UIImage,使用较大的图片或者图片使用情况较少使用imageWithContentsOfFile

    • imageWithContentsOfFile的用法

        NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”];  
        UIImage *image = [UIImage imageWithContentsOfFile:path];
      
  • 在模型类中(Model), 需要提供实例构造方法和对应的类构造方法。

  • 在模型类中(Model), 实例构造方法以init开头, 类构造方法以类名去掉前缀小写开头。

    • 例如

        // 例如类为 SAMessageModel
        @interface SAMessageModel : NSObject
          
          - (instancetype) initWithDict: (NSDictionary *)dict;
          
          + (instancetype) messageWithDict: (NSDictionary *)dict;
          
        @end
      
    • 在类方法的实现中,不要出现类名,全部用self进行替换。例如,上面例子的类方法实现中会出现 [[SAMessageModel alloc] initWithDict: dict] ; 我们需要写成[[self alloc] initWithDict: dict]。(这是为了解决继承时的类型不正确问题)

  • 在使用Masonry进行自动化布局的时候,你需要有如下类似格式:

      @implementation DIYCustomView
    
      - (id)init {
        self = [super init];
        if (!self) return nil;
    
        // --- Create your views here ---
        self.button = [[UIButton alloc] init];
    
        return self;
      }
    
      // tell UIKit that you are using AutoLayout
      + (BOOL)requiresConstraintBasedLayout {
          return YES;
      }
    
      // this is Apple's recommended place for adding/updating constraints
      - (void)updateConstraints {
    
          // --- remake/update constraints here
          [self.button remakeConstraints:^(MASConstraintMaker *make) {
              make.width.equalTo(@(self.buttonSize.width));
              make.height.equalTo(@(self.buttonSize.height));
          }];
    
          //according to apple super should be called at end of method
          [super updateConstraints];
      }
      
      - (void)didTapButton:(UIButton *)button {
          // --- Do your changes ie change variables that affect your layout etc ---
          self.buttonSize = CGSize(200, 200);
      
          // tell constraints they need updating
          [self setNeedsUpdateConstraints];
      }
    

@end



*注:还有需要的请自行添加 在CHANGELOG.md中进行说明*