iOS代码规范

作者: Suneday | 来源:发表于2017-07-06 13:03 被阅读88次

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 代码规范篇

    iOS 代码规范篇 iOS 代码规范篇

  • iOS Coding Style Guide 代码规范

    iOS Coding Style Guide 代码规范 iOS Coding Style Guide 代码规范

  • 乐乎-代码规范概述

    参考: 1、iOS中书写代码规范35条小建议 2、iOS开发总结之代码规范 3、iOS代码编程规范-根据项目经验汇...

  • iOS 代码规范文档

    iOS 代码规范文档 [toc] 修订 概述 制定目的:制定iOS 编码规范,主要是为了规范公司内部的iOS 代码...

  • iOS -如何让你写的代码更规范

    iOS -如何让你写的代码更规范 iOS -如何让你写的代码更规范

  • 代码之天圆地方

    优雅的写代码轻松的查代码严格的审代码 一、 iOS代码规范检查 新人入场第一件事就是熟悉代码规范iOS代码编程规范...

  • iOS开发总结之代码规范

    iOS开发总结之代码规范

  • iOS知识整理(二)

    OC知识整理 代码规范 iOS 代码规范 #pragma mark - 添加子视图#pragma mark - 添...

  • iOS 代码规范

    iOS规范:仅适用于iOS的代码规范(使用Objective-C语言)。 一.代码应该简洁易懂,逻辑清晰 不要过分...

  • iOS 团队编程规范

    iOS 团队编程规范 前 言 一、命名规范 二、代码注释规范 三、代码格式化规范 四、编码规范 参考资料: 转载自...

网友评论

    本文标题:iOS代码规范

    本文链接:https://www.haomeiwen.com/subject/ekshhxtx.html