代码组织
对内存的合理,controller,view的生命周期函数放到最上面,自己实现的方法在下面,相同/相近功能的方法采用#pragma mark -来标记
这样做的好处就是能快比较快速得定位到你要查找的代码段
pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
(void)didReceiveMemoryWarning {}
pragma mark - Custom Accessors
- (void)setCustomProperty:(id)value {}
(id)customProperty {}
pragma mark - IBActions
(IBAction)submitData:(id)sender {}
pragma mark - Public
(void)publicMethod {}
pragma mark - Private
(void)privateMethod {}
pragma mark - UITextFieldDelegate
pragma mark - UITableViewDataSource
pragma mark - UITableViewDelegate
pragma mark - NSCopying
(id)copyWithZone:(NSZone *)zone {}
pragma mark - NSObject
(NSString *)description {}
条件语句
应该:
if (user.isHappy) {
//Do something
} else {
//Do something else
}
if (!error) {
return success;
}
不应该:
if (user.isHappy)
{
//Do something
}
else {
//Do something else
}
if (!error)
return success;
命名
类和常量的命名应当使用前缀
比如:
UIViewController
static NSTimeInterval const RWTTutorialViewControllerAnimationDuration = 0.3;
UIViewController 后缀添加“ViewController”
UIView 后缀添加“View”
UIButton 后缀添加“Button”
UILabel 后缀添加“Label”
block
应该:
// blocks are easily readable
[UIView animateWithDuration:1.0 animations:^{
// something
} completion:^(BOOL finished) {
// something
}];
不应该:
// colon-aligning makes the block indentation hard to read
[UIView animateWithDuration:1.0
animations:^{
// something
}
completion:^(BOOL finished) {
// something
}];
注释
当需要注释时,注释应该用来解释这段特殊代码为什么要这样做。任何被使用的注释都必须保持最新或被删除。
一般都避免使用块注释。生成文档的注释除外
对生成文件,方法,属性的注释
推荐使用 VVDocumenter
使用效果:
/**
* 获取产品列别
*
* @param requestTag 请求标签
* @param finishBlock 请求结束处理,如果传出的数据为nil,就表示获取失败
*/
- (void)getProductTypeWithRequestTag:(int)requestTag finishBlock:(netRequestResultBlock)finishBlock{
}
方法命名规范
- (void)setExampleText:(NSString *)text image:(UIImage *)image;
- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;
- (id)viewWithTag:(NSInteger)tag;
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;
标题书写规范
//
// FINetWorkManager.m
// StudyClound
//
// Created by 陈江彬 on 6/3/15.
// Copyright (c) 2015 StudyClound. All rights reserved.
//
空1行
#import "FILoginViewController.h"
#import "AppDelegate.h"
空1行
@interface FINetWorkManager(){
空1行
ANReachability *wifiReach;
ANReachability *internetReach;
}
空1行
@property (nonatomic, strong) NSMutableArray *requestListArray;
空1行
@end
空1行
空1行
@implementation FINetWorkManager
变量
NSString text 既不是 NSString text 也不是 NSString * text
声明NSArray,NSDictionary,NSString这些属性一般使用copy,很少用strong,copy是复制一份新的,然后内存地址也就跟着变了,strong内存地址是不变的,如果此时A对象是从B对象赋值过来的,如果用了strong,B对象一旦改变,A对象的值也会跟着改变。用copy的话就不会出现这样子的情况。所以要分清楚情况。
@property (copy, nonatomic) NSString *tutorialName;
@property (copy, nonatomic) NSArray *array;
@property (copy,nonatomic) NSDictionary *dictionary;
字面值
NSString、NSDictionary、NSArray和NSNumber的字面值应该在创建这些类的不可变实例时被使用。请特别注意nil值不能传入NSArray和NSDictionary字面值,因为这样会导致crash。
应该:
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingStreetNumber = @10018;
不应该:
NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingStreetNumber = [NSNumber numberWithInteger:10018];
常量
常量应该使用static来声明而不是使用#define,除非显式地使用宏
应该:
static NSString * const RWTAboutViewControllerCompanyName = @"RayWenderlich.com";
static CGFloat const RWTImageThumbnailHeight = 50.0;
不应该:
#define CompanyName @"RayWenderlich.com"
#define thumbnailHeight 2
枚举类型
typedef NS_ENUM(NSInteger, RWTLeftMenuTopItemType) {
RWTLeftMenuTopItemMain,
RWTLeftMenuTopItemShows,
RWTLeftMenuTopItemSchedule
};
typedef NS_ENUM (NSInteger,RunGoalTypeE){
kRunGoalTypeNone = 0, //无目标
kRunGoalTypeTime = 1, //以时间为目标
kRunGoalTypeDistance = 2, //以距离为目标
kRunGoalTypeCalori = 3, //以消耗卡路里为目标
};
类构造方法
使用instancetype,而不是id
@interface Airplane
+ (instancetype)airplaneWithType:(RWTAirplaneType)type;
@end
黄金路径
当使用条件语句编码时,左手边的代码应该是”golden” 或 “happy”路径。
也就是不要嵌套if语句。
应该:
- (void)someMethod {
if (![someOther boolValue]) {
return;
}
//Do something important
}
不应该:
- (void)someMethod {
if ([someOther boolValue]) {
//Do something important
}
}
单例
单例对象应该使用线程安全模式来创建共享实例。
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
换行符
self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers product:productName price:price];
一行很长的代码应该分成多行代码。
self.productsRequest = [[SKProductsRequest alloc]
initWithProductIdentifiers:productIdentifiers
product:productName
price:price];
图片命名规则
采用“模块+功能”命名法
_icon 图标
_btn 按钮
_bg 背景
比如:用户中心的收藏图标,命名可以为:user_collect_icon.png
user_collect_btn.png //常态
user_collect_B_btn.png //选中状态
基本原则
1、所有类、方法、属性等命名,做到见名知意,采用驼峰式命名规则
2、所有方法与方法之间空1行,所有代码块之间空1行
3、 添加必要的注释
* 所有 .h 文件中的property 需要给出注释
* 所有自定义的方法需要给出注释
* 比较大的代码块需要给出注释
* 所有代码中出现的阿拉伯数字需要给出注释
* 程序中出现加密/解密 逻辑的操作地方,需要给出注释说明加密过程
4、删除未被使用的资源文件
5、相同的逻辑方法定义避免在多个地方出现,尽量将公用的类、方法抽取出来,比如支付跟分享最好是写在单独的一个类里,而不是直接写在一个ViewController。
6、对其他项目中copy过来的代码,根据具体需要更新代码风格,及时删除未被使用的代码
7、项目中所有Group或者文件名称(图片名字等),不要使用汉字命名,应使用英文命名。
8、项目中所有Group都需要在项目目录中存在一个真实的目录,Group中的文件与真实目录中文件一一对应
9、一个文件里,代码不要太长,ViewController的TableCell、代码量多的子View应当独立出来写在另一个文件
10、用代码写界面,尽量不使用xib、storyBoard