iOS代码规范

iOS代码规范

命名规范

1.0 对于常量的命名最好在前面加上字母k作为标记. 如:

static const NSTimeInterval kAnimationDuration = 0.3;

定义作为NSDictionary或者Notification等的Key值字符串时加上const关
键字, 以防止被修改. 如:

NSString *const UIApplicationDidEnterBackgroundNotification

Tips:

<pre><code>
I. 若常量作用域超出编译单元(实现文件), 需要在类外可见时, 使用
extern关键字, 并加上该类名作为前缀. 如 extern NSString *const
PGThumbnailSize
II.全局常量(通知或者关键字等)尽量用const来定义. 因为如果使用宏定
义, 一来宏可能被重定义. 二来引用不同的文件可能会导致宏的不同. P.S.
对于#define也添加一下前缀k(其实有点强迫症, 哈哈...)
</code></pre>

2.0 对于变量的命名规范
Tip:iOS命名两大原则是:可读性高和防止命名冲突(通过加前缀来保证). Objective-C 的命名通常都比较长, 名称遵循驼峰式命名法.

  • 实例
    <pre><code>
    //字符串声明后缀以Str结尾
    NSString *phoneStr;
    //错误示例
    NSString *str1;//不应以数字作为变量后缀,变量声明应具有一定的可识别程度
    </pre></code>

  • 变量后缀

<pre><code>
<>NSString; ----- Str,
NSMutableString ---- StrM;
NSArray; ----- Arr,
NSMutableArray ----- ArrM;
NSDictionary ---- Dic,
NSMutableDictionary ----DicM,
UILabel ----lab,
UIButton ----btn,
UITableView ---- tab,
UICollectionView ---- CollView,
UITextFiled ----TF,
UITextView ---- TV,
UIView ---- View,</pre></code>

  • 枚举变量的声明规范

建议使用基于Objective -c的枚举,更易读,枚举里面的类型尽量使用全部的大写,区分的话可用下划线后单词进行区别

<pre><code>
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
</pre></code>

不建议使用基于C的枚举

<pre><code>
typedef enum : {
CameraModeFront,
CameraModeLeft,
CameraModeRight,
} CameraMode
</pre></code>

条件判断

<li> 单层的判断语句不建议使用if else 逻辑语句</li>

不建议 <pre><code>
if (条件)else {
.....
}</pre></code>

建议使用三目运算符
<pre><code>result = object ? : [self createObject]; </pre></code>

<li>嵌套判断 </li>

<pre><code>
if (!user.UserName) return NO;
if (!user.Password) return NO;
if (!user.Email) return NO;
return YES;</pre></code>

不应该:实例

<pre><code>
BOOL isValid = NO;
if (user.UserName)
{
if (user.Password)
{
if (user.Email) isValid = YES;
}
}
return isValid;</pre></code>

判断条件的编写

<li> 判断条件在后 </li>
<pre><code>if(self.name = nil) </pre></code>

<li>错误写法</li>
<pre><code>if(nil = self.name)</pre></code>

初始化方法

<li> 初始化变量时候尽量使用字面量语法进行初始化</li>
<pre><code>
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve"];
NSDictionary *productManagers = @{@"iPhone" : @"Kate", @"iPad" : @"Kamal"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingZIPCode = @10018;</pre></code>

第一个好处是简洁, 第二个好处是可以防止初始化进去nil值造成crash

方法的写法

<li>方法里面参数过多的时候</li>

<pre><code>

  • (void)registerUserName:(NSString *)userName
    password:(NSString *)password
    email:(NSString *)email
    {
    // to do...
    }
    </pre></code>

Block的循环使用问题

<li>block内部使用弱引用</li>

<pre><code>
__weak typeof(self) weakSelf = self;
dispatch_block_t block = ^{
[weakSelf doSomething]; // weakSelf != nil
// preemption, weakSelf turned nil
[weakSelf doSomethingElse]; // weakSelf == nil
};
</pre></code>

以上那样写并不完美,block体里面的self是weak的, 所以就有可能在某一个时段self已经被释放了, 这时block体里面再使用self那就是nil
解决方法很简单, 就是在block体内define一个strong的self, 然后执行的时候判断下self是否还在, 如果在就继续执行下面的操作, 否则return或抛出异常
<pre><code>
__weak typeof(self) weakSelf = self;
myObj.myBlock = ^{
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf doSomething]; // strongSelf != nil
// preemption, strongSelf still not nil
[strongSelf doSomethingElse]; // strongSelf != nil
}
else {
// Probably nothing...
return;
}
};
</pre></code>

避免庞大的xib

可分为多个模块进行,xib在进行编译的时候会把所有的资源文件加载一次

赋值语句

赋值语句等号左右各加一个空格

错误示例
<pre><code>NSString *str=@"1";
</pre></code>
正确示例
<pre><code>NSString *str = @"1";</pre></code>

未完待续......

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS编程规范0规范 0.1前言 为􏰀高产品代码质量,指导广大软件开发人员编写出简洁、可维护、可靠、可 测试、高效...
    iOS行者阅读 4,508评论 21 35
  • 概要 Objective-C是一门面向对象的动态编程语言,主要用于编写iOS和Mac应用程序。关于Objectiv...
    DreamMmMmM阅读 1,197评论 0 7
  • 这里有些关于编码风格Apple官方文档,如果有些东西没有提及,可以在以下文档来查找更多细节: The Object...
    Loki9527阅读 406评论 0 0
  • 前言 最近突发奇想,想到之前公司培训的代码规范,所以简单的整理一下,希望能对大家有所帮助。 正文 部分一 1. 命...
    SwordDevil阅读 596评论 0 1
  • --force --nodeps 安装旧版本方法 rpm https://www.cnblogs.com/huac...
    AbnerSky阅读 402评论 2 3