iOS代码规范

2018-12-03  本文已影响7人  xiao小马哥

公司代码规范(创业邦内部使用)

命名基础


在⾯面向对象软件库的设计过程中,开发人员经常忽视对类,方法,函数,常量以及其他编程接⼝元素的命名。本节讨论大多数Cocoa接⼝的一些命名约定。

前缀

所有类名、枚举、结构、protocol定义时,都加上全大写的CYB作为前缀

后缀

所有protocol定义时,都加上后缀Delegate。如,CYBRefreshViewDelegate,表示RefreshView的协议

导入头文件

对于Objective-C的类文件,使用#import导入;对于c,c++文件,使用#include导入

方法命名

方法名首字母小写,其他单词首字母大写,每个空格分割的名称以动词开头。如:

  - (void)insertModel:(id)model atIndex:(NSUInteger)atIndex;

属性、变量命名

每个属性命名都加上类型后缀,如,按钮就加上Button后缀,模型就加上Model后缀。eg:

@property (nonatomic, strong) UIButton *submitButton;

对于内部私有变量,也需要加上后缀并以下划线开头,如:_titleLabel, _descLabel, _okButton

一般性原则

最好是既清晰⼜又简短,但不要为简短⽽而丧失清晰性

代码 点评
insertObject:atIndex: Good
insert:at: 不清晰;要插⼊什么?“at”表⽰示什么?
removeObjectAtIndex: Good
removeObject: 不错,因为⽅法是⽤用来移除作为参数的对象
remove: 不清晰;要移除什么?

名称通常不缩写,即使名称很⻓,也要拼写完全

代码 点评
destinationSelection Good
destSel 不清晰
setBackgroundColor: Good
setBkgdColor: 不清晰

一致性

尽可能与Cocoa编程接⼝命名保持一致。如果你不太确定某个命名的⼀致性,请浏览头文件或参考文档中的范例,在使⽤多态方法的类中,命名的⼀致性⾮常重要。在不同类中实现相同功能的⽅法应该具有同的名称。

代码 点评
- (NSInteger)tag 在 NSView, NSCell, NSControl 中有定义
- (void)setStringValue:(NSString *) 在许多 Cocoa classes 中都有定义

访问方法

访问方法是对象属性的读取与设置⽅法。其命名有特定的格式,依赖于属性的描述内容。

  - (type)noun;
  - (void)setNoun:(type)aNoun;

例如:

  - (NSString *)title;
  - (void)setTitle:(NSString *)aTitle;


  - (BOOL)isAdjective;

  - (void)setAdjective:(BOOL)isAdjective;

例如:

 - (BOOL)isEditable;
 - (void)setEditable:(BOOL)isEditable;

   - (BOOL)verbObject;

   - (void)setVerbObject:(BOOL)flag;

例如:

  - (BOOL)showsAlpha;
  - (void)setShowsAlpha:(BOOL)flag;

动词要用现在时时态

  - (BOOL)openFile:(NSString *)fullPath withApplication:
(NSString *)appName andDeactivate:(BOOL)flag;

  - (void)setAcceptsGlyphInfo:(BOOL)flag;
// 不好
  - (BOOL)acceptsGlyphInfo;

- (void)setGlyphInfoAccepted:(BOOL)flag;
// 不好
- (BOOL)glyphInfoAccepted;

// 好
- (void)setCanHide:(BOOL)flag;
- (BOOL)canHide;
- 
// 好
- (void)setShouldCloseDocument:(BOOL)flag;
// 不好
- (BOOL)shouldCloseDocument;

// 好
  - (void)setDoesAcceptGlyphInfo:(BOOL)flag;
  - (BOOL)doesAcceptGlyphInfo;

只有在方法需要间接返回多个值的情况下,才使⽤get
像上面这样的方法,在其实现里应允许接受NULL作为其in/out参数,以表示调⽤者对⼀个或多个 返回值不感兴趣。

委托方法

委托方法是那些在特定事件发⽣生时可被对象调⽤用,并声明在对象的委托类中的方法。它们有独特的命名约 定,这些命名约定同样也适⽤用于对象的数据源方法。

    -(BOOL)tableView:(NSTableView*)tableView shouldSelectRow:(int)row;
    - (BOOL)application:(NSApplication *)sender openFile:(NSString
*)filename;
  - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender;
   - (void)windowDidChangeScreen:(NSNotification *)notification;
   - (void)browserDidScroll:(NSBrowser *)sender; 
   - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;

  - (BOOL)windowShouldClose:(id)sender;

枚举常量


typedef NS_ENUM(NSUInteger, CYBContentMode) {
  kContentModeScaleFit = 1 << 1,
  kContentModeScaleFill = 1 << 2
};

const常量

以小写k开头,后面单词首字母大写,其余小写。如:

const float kMaxHeigt = 100.0f;

如果是静态常量,仅限本类内使用的,加上前缀s_,如果是整个工程共用,以sg_为前缀。如:

s_kMaxHeight; sg_kMaxHeight;

其他常量

#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kLoginSuccessNotification @”CYBLoginSucessNotification”

代码注释

类注释

类头需要有注释,功能说明,作者等:如,

/**
 *   这里是功能说明
 *
 *  @author 
 *  @modify 如果有修改需要这行,加上修改人和修改日期
 */
@interface CYBUIMaker : NSObject

方法注释

方法注释需要加上作者,功能说明,参数说明,返回值说明:

/**
 *  @author
 *  描述内容
 *
 *  @param string <#string description#>
 *  @param font   <#font description#>
 *
 *  @return <#return value description#>
 */
+ (CGSize)sizeWithString:(NSString*)string andFont:(UIFont *)font;

块注释

对于块注释,有多种多种风格

风格一:
/////////////////////////////////////
// @name UIButton控件生成相关API
/////////////////////////////////////
风格二:
//
// 块功能说明
//

风格三:

/*
* 块功能说明
*
*/

类内分块结构写法

有生命周期的类,从上到下结构为:

#pragma mark – life cycle
#pragma mark – Delegate
#pragma mark – CustomDelegate…
#pragma mark – Event
#pragma mark - Network
#pragma mark – Private
#pragma mark – Getter/Setter

参考

本文档参考[Coding Guidelines for Cocoa](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ CodingGuidelines/CodingGuidelines.html)

import规范

当一个Controller或者一个Class中需要用到不同的类和Define时, 我们应当把#import划分.
划分原则: 哪个Controller或者Class是本Controller或者Class的次级就放在一起, 公共Controller或Class就与之前的空一行, 紧跟着下面.
比如:

#import "AspManageServicePasswordViewController.h"
#import "AspResetServicePasswordViewController.h"
#import "AspServicePasswordView.h"

#import "NorNavShareView.h"

#import "AppDelegate.h"
#import "BCTabBarView.h"

#import "ModifyPwdViewController.h"
#import "AspLoginVC.h"

# Define规范

使用#define时, 必须要以数值对齐.
比如:

#define kLabelSize               20
#define kLabelMargins            20

#define kConfirmBottom           63

错误写法:

#define kLabelSize 20
#define kLabelMargins 20

#define kConfirmBottom 63

使用#define定义预编译宏时, 应当把宏定义成全部大写字母, 且与“_”分隔, 此方法是苹果官方所推荐.
比如:

#define NS_AVAILABLE(_mac, _ios) CF_AVAILABLE(_mac, _ios)
#define NS_AVAILABLE_MAC(_mac) CF_AVAILABLE_MAC(_mac)
#define NS_AVAILABLE_IOS(_ios) CF_AVAILABLE_IOS(_ios)

错误写法:

#define kCancelBottom  25
#define kCancelHeight  44
#define kCancelMargins 25

在代码中尽量减少在代码中直接使用数字常量, 而使用#define来进行声明常量或运算.
比如:

#define kTableViewHeight     300
#define kTableViewCellHeight label.height + view.height

错误写法:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.frame = CGRectMake(0, 64, 320, 660);
}

尽量代码中得重复计算, 比如在代码中很多地方需要使用到屏幕的宽度, 然后再进行计算, 我们就可以把屏幕的宽度进行抽取封装成#define.
比如:

#define AspScreenWidth   ([[UIScreen mainScreen] bounds].size.width)
#define kTableViewHeight 300
#define NAV_VIEW_HEIGHT  64

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.frame = CGRectMake(0, NAV_VIEW_HEIGHT, AspScreenWidth, kTableViewHeight);
}

错误写法:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.frame = CGRectMake(0, 64, 320, 660);
}

paragma Mark 规范

使用#pragma mark时紧挨着方法名, 且与上一个方法的间距为1空行.
比如:

- (void)mineViewModel {

    // Code Body
}

#pragma mark - TableViewRefresh
- (void)pulldownRefresh:(AspTableView *)tableView {

    [self.mineViewModel requestDataWithSuccess:nil failure:nil];
}

错误写法:

- (void)mineViewModel {

    // Code Body
}

#pragma mark - TableViewRefresh

- (void)pulldownRefresh:(AspTableView *)tableView {

    [self.mineViewModel requestDataWithSuccess:nil failure:nil];
}

- (void)mineViewModel {

    // Code Body
}
#pragma mark - TableViewRefresh
- (void)pulldownRefresh:(AspTableView *)tableView {

    [self.mineViewModel requestDataWithSuccess:nil failure:nil];
}

当两个方法相近或者都是同一个模块里的方法, 使用#pragma mark加注释来导航代码.
比如:

#pragma mark - Some Mothed
- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;

错误写法:

#pragma mark - 
- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;


- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;

@interface规范

在@Interface里遵循的Delegate, 或者是DataSource方法, 必须以,间隔, 且空一格.
比如:

@interface AspEasyOwnResetPasswordViewController () <UITextFieldDelegate, MBAlertViewDelegate, UITableVIewDelegate, UITextViewDelegate, UIAlertViewDelegate, UITableViewDataSource>

错误写法:

@interface AspEasyOwnResetPasswordViewController()<UITextFieldDelegate,MBAlertViewDelegate>

当我们在@interface里声明变量时, 我们应该根据内存管理关键字来划分模块, 且以 “*” 分类对齐(基础数据类型除外).
比如:

@interface AspResetServicePasswordViewController () <UITextFieldDelegate, MBAlertViewDelegate>

@property (nonatomic, strong) AspRetrievePasswordView *retrievePassword;
@property (nonatomic, strong) UITextField             *foucsTextField;

@property (nonatomic, copy) NSString *brandName;
@property (nonatomic, copy) NSString *brandType;

@property (nonatomic, assign) NSTimer *timer;

@property (nonatomic, assign) NSInteger zeroCount;
@property (nonatomic, assign) NSInteger verificationCode;

@end

错误写法:

@property (nonatomic, strong) NSDictionary *everyDataDic;
@property (nonatomic, copy) NSString *timeStr;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) SHLineGraphView *lineGraph;

在@interface里声明@property需要把nonatomic放在前(使用xib的除外), strong等内存关键字放在后, 且“()”前后各空一格.
比如:

@property (nonatomic, strong) UILabel        *unitLabel;
@property (nonatomic, strong) UIImageView    *bgView;
@property (nonatomic, strong) NSMutableArray *dayDataObjects;

错误写法:

@property ( nonatomic, retain ) UIScrollView*   navScrollView;
@property ( nonatomic, retain ) NSMutableArray* ItemBtns;

@implementation规范

禁止使用@synthesize关键字, @property已经自动帮我们声明了Setter和Getter方法, 如有特殊需求可重写Setter方法, 所以不需要再使用@synthesize.
错误写法:

@implementation FreeExperienceView
@synthesize segment    = _segment;
@synthesize dataInfo   = _dataInfo;
@synthesize delegate   = _delegate;
@synthesize MyEXPView  = _MyEXPView
@synthesize canEXPView = _canEXPView;
@synthesize isRefresh  = _isRefresh;

实例规范

一个实例变量出现属性赋值时, 如果是以“=”赋值, 必须以“=”对齐, 如果以“[]”赋值, 必须以开头第一个“[”对齐.
比如:

- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate {

    self.phoneTextFieldOne.delegate   = textFieldDelegate;
    self.phoneTextFieldTwo.delegate   = textFieldDelegate;
    self.phoneTextFieldThree.delegate = textFieldDelegate;
    self.reviewPassword.delegate      = textFieldDelegate;
    self.confirmPassword.delegate     = textFieldDelegate;
}

错误写法:

- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate {

    self.phoneTextFieldOne.delegate = textFieldDelegate;
    self.phoneTextFieldTwo.delegate = textFieldDelegate;
    self.phoneTextFieldThree.delegate = textFieldDelegate;
    self.reviewPassword.delegate = textFieldDelegate;
    self.confirmPassword.delegate = textFieldDelegate;
}

当出现多个实例变量时, 应该使用代码块的思想, 把它们组成一个模块一个模块(特殊情况除外)比如需要局部声明的AVFoundation.
比如:

- (void)customFunction {

    Person *person = [[Person alloc] init];
    person.name    = @"xiaoming";
    person.age     = 17;

    Car *newCar = [[Car alloc] init];
    newCar.name = @"保时捷";
}

错误写法:

- (void)customFunction {

    Person *person = [[Person alloc] init];
    person.name    = @"xiaoming";
    person.age     = 17;
    Car *newCar = [[Car alloc] init];
    newCar.name = @"保时捷";
}

禁止在实例对象, 赋值, 调用方法后的“;”与前面代码空格.
比如:

- (void)customFunction:(NSString *)someName {

    self.userName = someName;    
}

错误写法:

- (void)customFunction:(NSString *)someName {

    self.userName = someName ;
}

在制类型转换时, 函数强转类型与被强转函数名之间不放置空格, 但类型中的Type与*必须空格.
比如:

NSString *userName = (NSString *)student;

错误写法:

NSString *userName = (NSString *) student;

NSDictionary规范

当NSDictionary里的Key : Value数量大于或者等于2时, 应拆分为多行显示, 并以 ":" 对齐.
比如:

NSDictionary *people = @{@"xiaoming" : 18,
                         @"xiaohong" : 19,
                         @"xiaowang" : 20};         

错误写法:

NSDictionary *people = @{@"xiaoming":18,@"xiaohong":19,@"xiaowang":20};         

NSArray规范

当NSArray里的元素数量大于或者等于2时, 应拆分为多行显示, 并以第一个元素对齐.
比如:

NSArray *nameArray = @[@"小明", 
                       @"小红, 
                       @"小王"];         

错误写法:

NSArray *nameArray = @[@"小明",@"小红",@"小王"];         

函数规范

在声明函数时“-”与“(type)”必须空一格, 且后面的参数类型与“*”也必须空一格.
比如:

- (void)customFunction {

    // Code Body
}

错误写法:

-(void)customFunction {

    // Code Body
}

-(void) customFunction {

    // Code Body
}

方法与方法之间相隔一行的间距, 禁止不空行或多空行.
比如:

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        //Code Body
    }    
    return self;
}

- (void)customFunction {
    //Code Body
}

错误写法:

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        //Code Body
    }    
    return self;
}
- (void)customFunction {
    //Code Body
}

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        //Code Body
    }    
    return self;
}



- (void)customFunction {
    //Code Body
}

在每个方法定义前需要留白一行, 也就是每个方法之间留空一行, 如果是系统的方法则可以不留空.
比如:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Code Body
}

错误写法:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Code Body
}

- (void)customFunction {
    [self customFunction];

    // Code Body
}

在函数体内, 如果第一句调用的是系统方法, 可不换行, 如果是自定义的方法(包括if-else和for-in), 必须换行.
比如:

- (void)mineViewModel {

    [self customFunction]
}

错误写法:

- (void)mineViewModel {
    [self customFunction]
}

在函数体里, 如果只有一句代码, 可与函数名紧挨
比如:

- (NSString *)iconImageName {
    return @"icon_nav5";
}

错误写法:

- (NSString *)iconImageName {   

    return @"icon_nav5";
}

任意行代码不能超过80个字符, 如果超过80个字符, 可以考虑多行显示, 比如有多个参数时, 可以每个参数放一行.
比如:

- (void)replaceObjectsInRange:(NSRange)range 
         withObjectsFromArray:(NSArray<ObjectType> *)otherArray 
                        range:(NSRange)otherRange; 

错误写法:

- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray range:(NSRange)otherRange; 

如果实现的是空函数体, 那么就不需要空格.
比如:

- (void)showView {}

错误写法:

- (void)showView {
}

- (void)showView {

}

if-else规范

如果在方法中出现if-else判断, 我们应该使用结构清晰的排版方式, if-else语句与方法名空行, 与return语句可以不用空行.
比如:

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        // code body
    }
    return self;
}

错误写法:

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // code body
    }
    return self;
}

if-else内部也必须跟着空行, 在else之后的"}"可不空行.
比如:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (age < 0) {

        // Code Body        
    }
}

错误写法:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (age < 0) {
        // Code Body
    }
}

if-else超过四层的时候, 就要考虑重构, 多层的if-else结构很难维护.
比如:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (condition) {

        // Code Body

    } else if (condition){

        // Code Body

    } else if (condition){

        // Code Body

    } else if (condition){

        // Code Body

    } else if (condition){

        // Code Body

    } else if (condition){

        // Code Body

    } else {

        // Code Body
    }
}

合理使用if-else, 当某个场景只需满足一个条件才能进入if-else时, 应该把if-else抽成一个模块.
比如:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (age < 0) {

        NSLog(@"这是错误的年龄");

        return;
    }

    NSLog(@"现在时%ld岁", (long)age):
}

错误写法:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (age < 0) {

        NSLog(@"这是错误的年龄");

    } else {

        NSLog(@"现在时%ld岁", (long)age):
    }
}

在使用if-else时必须加"{}"符号, 如果存在else时, 必须加{}
比如:

- (void)customFunction {

    if (condition) {

        // Code Body
    } else {

        // Code Body
    }
}

错误写法:

- (void)customFunction {

    if (condition) // Code Body

    else // Code Body
}

- (void)customFunction {

    if (condition) // Code Body

    // Code Body
}

在使用if-else中, ()中的条件, 不需要与左右两边的()空格, 且{}与前一个)空格
比如:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self){

       [self setBackgroundColor:TRAFFICDETAILBACGROUNDCOLOR];

        if (!_ItemBtns) {

           _ItemBtns = [[NSMutableArray alloc] init];
        }

        _mnScrollViewWidth = kWidthToFit(94); 

        self.colorNoSel = UIColorFromRGB(0xdee6ea);
        self.colorSel   = UIColorFromRGB(0xcdd0d2);

        [self addSubview:self.navScrollView];
    }

    return self;
}

错误写法

- (id) initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if ( self )
    {
       [self setBackgroundColor:TRAFFICDETAILBACGROUNDCOLOR];

        if ( !_ItemBtns ) {
           _ItemBtns = [[NSMutableArray alloc] init] ;
        }
        _mnScrollViewWidth = kWidthToFit(94); 
        self.colorNoSel = UIColorFromRGB(0xdee6ea) ;
        self.colorSel = UIColorFromRGB(0xcdd0d2) ;

        [self addSubview:self.navScrollView];
    }

    return self;
}

For-In & For 规范

当函数中需要使用到"for(for-in)", 在"for(for-in)"内, 必须和上文的"if-else"一样空行, 与return语句可不空行.
比如:

- (void)customFunction {

    for (NSInteger i = 0; i < 10; i++) {

        // code body
    }
}

错误写法:

- (void)customFunction {
    for (NSInteger i = 0; i < 10; i++) {
        // code body
    }
}

在"for(for-in)"中, 里面的参数必须严格按照上文所示的实例规范来声明, 且是兼容64位的类型.
比如:

- (void)viewDidLoad {
    [super viewDidLoad];

    for (NSInteger i = 0; i < 10; i++) {

        // code body
    }
}

错误写法:

- (void)viewDidLoad {
    [super viewDidLoad];

    for (NSInteger i=0;i<10;i++) {
        // code body
    }
}

当函数中需要使用到"for(for-in)", 如果该"for(for-in)"是在函数的第一句, 必须和上文的"if-else"一样空行, 与return语句可不空行.
比如:

- (void)customFunction {

    for (NSInteger i = 0; i < 10; i++) {

        // code body
    }
}

错误写法:

- (void)customFunction {
    for (NSInteger i = 0; i < 10; i++) {
        // code body
    }
}

Block规范

在函数中使用到Block时, 与if-else或者for-in不太一样, Block第一行与代码块必须得空行, 无论方法是否是系统自带的.
比如:

- (void)customFunction {

    [className blockName:^(parameter) {

        // Code Body
    }];
}

错误写法:

- (void)customFunction {

    [className blockName:^(parameter) {
        // Code Body
    }];
}

运算符规范

一元运算符和参数之间不放置空格, 比如"!"非运算符, "&"安位与, "|"安位或.
比如:

BOOL isOpen  = YES;
BOOL isClose = !isOpen;

错误写法:

BOOL isOpen  = YES;
BOOL isClose = ! isOpen;

BOOL isOpen=YES;
BOOL isClose=!isOpen;

二元运算符和参数之间要有空格, 如赋值号"="左右各留一个空格.
比如:

self.myString = @“mySring”;

错误写法:

self.myString=@"myString";

三目运算符和参数之间必须得有空格, 如判断符"?"结果符":"
比如:

NSInteger userAge = @"Man" ? 18 : 19;

错误写法:

NSInteger userAge = @"Man"?18:19;

命名规范

实例命名规范

使用完整的英文描述来准确描述Variable /Class /Define /Notes, 禁止使用拼音命名, 不建议建议使用缩写.
比如:

NSString *userName = @"小明";

错误写法:

NSString *uName = @"小明";

使用本行业的专业术语, 如果是消费者的话就要使用消费者对应的名词, 用户的话就使用用户对应的名词, 并且以“=”对齐, 禁止夸域命名.
比如:

NSString *userName = @"小明";
NSInteger userAge  = 18;
CGFloat userHeight = 170.0f;

错误写法:

NSString *uName = @"小明";
NSInteger uAge  = 18;
CGFloat uHeight = 170.5f;

使用Apple推荐的驼峰命名方式来编写代码, 方法开头第一个字母必须小写(特殊命名除外), 禁止使用随性的命名方式.
比如:

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;

错误写法:

- (CGPoint)ConverTPoint:(CGPoint)POINT ToView:(nullable UIView *)view;

@Property命名规范

尽量少用缩写, 使用Apple推荐的短句命名法命名且第一个字母必须小写, 这样子做, 可以省略再注释处理.
比如:

@property (readonly, copy) NSString *decomposedStringWithCanonicalMapping;
@property (readonly, copy) NSString *precomposedStringWithCanonicalMapping;
@property (readonly, copy) NSString *decomposedStringWithCompatibilityMapping;
@property (readonly, copy) NSString *precomposedStringWithCompatibilityMapping;

错误写法:

@property (readonly, copy) NSString *uName;
@property (readonly, assign) NSInteger *uAge;
@property (readonly, assign) CGFloat *uHeight;

@Interface->class命名规范

尽量使用有意义的英文名字命名, 拒绝使用"i", "j"等无意义的字符命名, 类的命名首字母大写, 其他变量的命名首字符小写.
比如:

@interface People : SuperClassName
@end

错误写法:

@interface people : SuperClassName
@end

@interface ren : SuperClassName
@end

Block命名规范

尽量使用有意义的命名方法, 禁止使用简称去命名.
比如:

@property (nonatomic, copy) void(^aspWebNavigationRightBtnBlock)(parameter);

错误写法:

@property (nonatomic, copy) void(^aspWNavRBBlock)(parameter);

for-in命名规范

在使用快速遍历for-in时, 其中的参数需要根据对应的元素进行一一对应来命名.
比如:

NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];

for (id number in numberArray) {

    NSLog(@"%@", number);
}

错误写法:

NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];

for (id item in numberArray) {

    NSLog(@"%@", item);
}

NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];

for (id abc in numberArray) {

    NSLog(@"%@", item);
}

注意: 在工程中少使用Xib, 禁止在StoryBoard拖入任何控件, 且StoryBoard只可作为项目架构所使用. 文件夹层次结构

上一篇下一篇

猜你喜欢

热点阅读