1OC代码规范一、修改记录:....................................................................................................................2二、参考资料:....................................................................................................................2三、编码规范:....................................................................................................................2文件注释:......................................................................................................................................2类的命名...........................................................................................................................................3Protocals..........................................................................................................................................3方法命名及调用:.........................................................................................................................4枚举的命名......................................................................................................................................4点用法:...........................................................................................................................................5空格:...............................................................................................................................................5block:.............................................................................................................................................5条件语句...........................................................................................................................................6三元运算子......................................................................................................................................7变量...................................................................................................................................................7常量...................................................................................................................................................7属性命名:......................................................................................................................................8初始化和内存释放.........................................................................................................................8Literals字面量...............................................................................................................................9CGRect函数-暂时不做限制....................................................................................................9BOOL陷阱.....................................................................................................................................10异常处理:....................................................................................................................................11调试Log.........................................................................................................................................11语言资源文件................................................................................................................................11Additional:.................................................................................................................................112OC代码规范一、修改记录:1,07-18初版by黄超强2,版本:2.0日期:07-223,修改记录:①增加相关规范:block,异常处理,语言配置strings文件,调试log打印②其他规范的补充以及修改:a,方法行数限制,方法注释b,枚举指明含义以及偏移c,条件语句if的格式规定d,switch的default条件处理e,初始化和内存释放f,CGRect暂不做限制g,实例变量前增加下划线二、参考资料:Apple:CodingGuidelinesforCocoa,TheObjective-CProgrammingLanguageGoogle:Objective-CStyleGuide三、编码规范:文件名命名规则:分类的文件名应该包含被扩展的类名,如:UIImageView+WebCache.h(目前Xcode已自动生成)文件注释:Xcode新建的文件默认有文件名,项目名称,创建者以及时间,版权信息,如:////ViewController.h//Alpha1S_NewInteractionDesign3////CreatedbyJesseon15/8/3.//Copyright(c)2015年Ubtechinc.Allrightsreserved.//a.建议增加的内容:b.简单描述该文件主要实现的功能,作用等c.如果是修改别人的文件且有重大改动,建议在作者一栏增加修改记录,修改日期,修改人,后续维护有问题方便联系。每行的长度▪每行最多不得超过100个字符▪Google的80字符的标准有点少,这导致过于频繁的换行(Objectve-C的代码一般都很长)通过“Xcode=Preferences=TextEditing=勾选ShowPageGuide/输入100=OK”来设置提醒类的命名类名(以及类别、协议名)应首字母大写,并以驼峰格式分割单词。1.类的前缀1)所有类名、枚举、结构、protocol定义时最好加一个统一的标示符,可以是项目缩写,或者个人项目的名称缩写,例如都加上全大写的UBT作为前缀2)根据功能模块可以在给功能模块的类添加功能模块的名称前缀2.类的后缀所有protocol定义时,都加上后缀Delegate,所有的控制器都加上Controller,所有的通知名都加上Notification。Protocals▪类型标示符、代理名称、尖括号间不留空格。▪该规则同样适用于:类声明、实例变量和方法声明。▪如果类声明中包含多个protocal,每个protocal占用一行,缩进4个字符。@public和@private使用单独一行,且缩进1个字符4方法命名及调用:方法名应在-OR+和返回值之间留1个空格,方法名和第一个参数间不留空格,遵守小驼峰原则,首字母小写,其他单词首字母大写,每个空格分割的名称以动词开头。执行性的方法应该以动词开头,小写字母开头,返回性的方法应该以返回的内容开头,但之前不要加get。如:-(void)insertModel:(id)modelatIndex:(NSUInteger)atIndex;-(instancetype)arrayWithArray:(NSArray*)array;当参数过长时,每个参数占用一行,以冒号对齐。如:-(void)doSomethingWith:(GTMFoo*)theFoorect:(CGRect)theRectinterval:(CGFloat)theInterval{...}方法的注释要标明此方法的含义,各个参数的意义,返回值(VVDocumenter-Xcode插件)方法的调用调用方法沿用声明方法的习惯。例外:如果给定源文件已经遵从某种习惯,继续遵从那种习惯。所有参数应在同一行中,或者每个参数占用一行且使用冒号对齐。如:[myObjectdoFooWith:arg1name:arg2error:arg3];或[myObjectdoFooWith:arg1name:arg2error:arg3];为保证视