设为首页 加入收藏

TOP

iOS开发代码规范(通用)(一)
2017-10-13 10:33:33 】 浏览:5698
Tags:iOS 开发 代码 规范 通用

1. 关于命名

 1> 统一要求

  • 含义清楚尽量做到不需要注释也能了解其作用,若做不到,就加注释

  • 使用全称,不适用缩写

 2> 类的命名

  • 大驼峰式命名:每个单词的首字母都采用大写字母

   例子:MFHomePageViewController

  • 后缀要求

  ViewController: 使用ViewController做后缀

       例子: MFHomeViewController

     View: 使用View做后缀

       例子: MFAlertView

     UITableCell:使用Cell做后缀

       例子: MFNewsCell

     Protocol: 使用Delegate或者DataSource作为后缀

       例子: UITableViewDelegate

     UI控件依次类推

 3> 私有变量

  • 小驼峰式命名:第一个单词以小写字母开始,后面的单词的首字母全部大写

  例子:firstNamelastName

  • _ 开头,第一个单词首字母小写

  例子:NSString * _somePrivateVariable

  • 私有变量放在 .m 文件中声明 

 4> property变量

  • 小驼峰式命名

   例子:///注释

      @property (nonatomic, copy) NSString *userName;

  • 禁止使用synthesize关键词

 5> 宏命名

  • 全部大写,单词间用 _ 分隔。[不带参数]

  例子: #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"

  • 以字母 k 开头,后面遵循大驼峰命名。[不带参数]

  例子:#define kWidth self.frame.size.width

  • 小驼峰命名。[带参数]

  #define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]

 6> Enum

  • Enum类型的命名与类的命名规则一致

  • Enum中枚举内容的命名需要以该Enum类型名称开头

  例子:

1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
2     AFNetworkReachabilityStatusUnknown = -1,
3     AFNetworkReachabilityStatusNotReachable = 0,
4     AFNetworkReachabilityStatusReachableViaWWAN = 1,
5     AFNetworkReachabilityStatusReachableViaWiFi = 2
6     };

 7> Delegate命名

  • 类的实例必须为回调方法的参数之一, 如

  -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

  • 回调方法的参数只有类自己的情况,方法名要符合实际含义, 如:

  -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

  • 以类的名字开头(回调方法存在两个以上参数的情况)以表明此方法是属于哪个类的, 如:

  -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • 使用didwill通知Delegate已经发生的变化或将要发生的变化, 如:

  -(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
  -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;

2. 私有方法及变量声明

 1> 声明位置

  在.m文件中最上方,定义空的category进行声明
   例子:

 1        #import "CodeStandardViewController.h"
 2     // 在这个category(类目)中定义变量和方法
 3     @interface CodeStandardViewController ()
 4     {
 5 
 6       // 声明私有变量
 7     }
 8 
 9      // 私有方法
10     - (void)samplePrivateMethod;
11     @end
12 
13     @implementation CodeStandardViewController
14     // 私有方法的实现
15     - (void)samplePrivateMethod
16     {
17       //some code
18     }

3. 关于注释

 最好的代码是不需要注释的 尽量通过合理的命名

 良好的代码把含义表达清楚 在必要的地方添加注释

 注释需要与代码同步更新

 如果做不到命名尽量的见名知意的话,就可以适当的添加一些注释或者mark

 1> 属性注释

  例子:
    /// 学生
    @property (nonatomic, strong) Student *student;

 2> 方法声明注释:

 1     /** 
 2    * @brief 登录验证
 3    *
 4    * @param personId 用户名
 5    * @param password 密码
 6    * @param complete 执行完毕的block
 7    *
 8    * @return
 9    */
10   + (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;

4. 关于UI布局

 使用Interface Builder进行界面布局

 Xib文件的命名与其对应的.h文件保持相同

 Xib文件中控件的组织结构要合理,Xib文件中控件需要有合理的可读性强的命名,方便他人理解

5. 格式化代码

 1> 指针 "*" 位置

  定义一个对象时,指针 "*" 靠近变量

   例子: NSString *userName;

 2> 方法的声明和定义

  在 - 、+ 和 返回值 之间留一个空格,方法名和第一个参数之间不留空格

- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{...}

 3> 代

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇天的故事 V1.0.2 下一篇在Swift中应用Grand Central Disp..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目