命名
所有的单词必须拼写完整,不可缩写!
善于使用介词:for/of/when/to/in/with/at等
1. 文件命名
项目及文件命名以Hori为前缀:
项目命名:HoriSmartCommunity
文件命名:HoriHttpApi.m
2. 属性、成员变量命名
1)第一个单词的首字母小写,从第二个单词开始的首字母大写:
@property (strong, nonatomic) NSString *phoneNumber;
2)名字最后需要跟上属性的类型,如UIButton,属性名最后需要跟上button,必须是全称,不得缩写,如:btn;NSString类型的属性名可以不写后缀:
@property (strong, nonatomic) UIButton *clickButton;
@property (strong, nonatomic) UITextField *phoneTextField;
@property (copy, nonatomic) NSArray *dataArray;
@property (copy, nonatomic) NSMutableArray *tableListMutableArray;
// NSString类型,可以不写后缀
@property (copy, nonatomic) NSString *password;
@property (strong, nonatomic) HoriWarningView *warningView;
3)名字要有意义,但可视情况而定;
// 如果整个文件中只有这么一个UIView,这写法是可以的;
// 但文件中有多个UIView,这写法是错误的!
@property (strong, nonatomic) UIView *view;
// 这个命名毫无意义,是不允许的!
@property (strong, nonatomic) UIImageView *aaaImageView;
// 文件中存在多个UIView,名字必须有意义
@property (strong, nonatomic) UIView *topView;
@property (strong, nonatomic) UIView *bottomView;
4)布尔属性命名,前面不需要is前缀,如果名字为形容词,通常指定带有is前缀的get方法提高可读性:
// 动词
@property (assign, nonatomic) BOOL adjustsImageWhenHighlighted;
// 动词
@property (assign, nonatomic) BOOL showsTouchWhenHighlighted
// 形容词,指定带有is前缀的get方法
@property (assign, nonatomic, getter=isSelected) BOOL selected;
5)成员变量名字前需要添加下划线:
@implementation HoriTestView {
NSString *_phoneNumber;
}
@end
3.方法命名
1)初始化方法前以setup为前缀
// 导航栏的初始化方法
- (void)setupNavigation {
}
// 数据的初始化
- (void)setupTableViewData {
}
2)方法参数前要加参数的名称提示
// 错误写法
- (void)init:(NSString *)name;
// 正确写法
- (void)initWithName:(NSString *)name;
3)对于有返回值的方法,表示取得某个对象,要以名词作为方法的开头,不要使用get开头
// 返回手机号
- (NSString *)phoneNumbeWithRecord:(HoriSipRecord *)sipRecord;
// 返回字符串的高度
- (CGFlot)stringHeightWithforFontOfSize:(CGFloat)fontSize stringMaxSize:(CGFloat)maxSize;
// 获取在rawString基础上添加rangeValue范围的随机数
- (NSString *)randomStringWithRawString:(NSString *)rawString rangeValue:(NSInteger)rangeValue;
// 获取yyyMMddHHmmss格式的字符串
- (NSString *)yyyMMddHHmmssFormtString;
// 手机号码的有效性
- (BOOL)phoneNumberValidity;
// 官方API
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
4)对于没有返回值的方法,表示执行某种操作,要以动词作为方法的开头
// 点击了登录按钮
- (void)didClickedLoginButton:(UIButton *)loginButton;
// 查找record
- (void)findRecordWithName:(NSString *)name;
// 官方API
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition;
4.委托命名(Delegate)
1)一个委托方法的第一个参数为触发它的对象,第一个关键词是触发对象的类名。根据委托方法触发的时机和目的,使用should、will、did等关键字:
@class HoriMonitorCell;
@protocol HoriMonitorCellDelegate <NSObject>
- (void)monitorCell:(HoriMonitorCell *)monitorCell
didClickMonitorWithPhoneBookRecord:(HoriSipPhoneBookRecord *)phoneBookRecord;
@end
5.块/闭包(Block)
如果一个block经常被使用,那须为这个block类型创建typedef,以Hori为前缀:
typedef int(^HoriSomeBlock) (BOOL flag, int value);
6.常量命名
1)定义一组相关的常量,尽量使用枚举,名字前缀Hori,内部成员常量名 = 枚举名 + 成员描述。最好使用NSENUM和_NS_OPTIONS宏来定义枚举类型
typedef NS_ENUM(NSInteger, HoriAppOpenType) {
// 枚举名 + 成员描述
HoriAppOpenTypeNetworking = 1,
HoriAppOpenTypeSonic,
}
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
2)单个常量的命名,不要使用#define来定义常量,用const修饰,错误写法:#define IPHONE6Puls_WIDTH 414
- 若常量局限于某个实现文件之内,则在前面加上字母
k; - 若常量在类之外可见,则通常以类名为前缀。
static CGFlot const HoriIPhone6PulsWIDTH = 414;
static NSString *const HoriLoginAccount = @"123456";
// 声明常量
extern NSString *const HoriBaseRequestUrl;
// 定义
NSString *const HoriBaseRequestUrl = @"https://tt.hori-gz:8080";
// cell的ID,只是在单个实现文件内可见,则用字母k开头
static NSString *const kTableViewCellIdentity = @"tableViewCellID":
7.通知命名(Notification)
通知常用于模块间的消息传递,所以通知要尽可能地表示出发生的事件,命名规范:
Hori + [触发通知的类名] + [Did | Will] + [动作] + Notification
如:
// sip注册完成
HoriSipRegistrationCompletionNotification
// 官方通知
NSUserDefaultsDidChangeNotification
NSFileHandleReadToEndOfFileCompletionNotification
声明通知使用NSNotifcationName修饰:
// 声明通知
extern NSNotificationName const HoriSipRegistrationCompletionNotification;
// 实现
NSNotificationName const HoriSipRegistrationCompletionNotification = @"HoriSipRegistrationCompletionNotification";
8.分类命名(Category)
分类名称前加上Hori前缀
错误写法:
@interface NSString (HTTP)
// encode a string with url encoding
- (NSString *)urlEncodeString;
// decode a url encoded string
- (NSString *)urlDecodeString;
@end
正确写法:
@interface NSString (Hori_HTTP)
// encode a string with url encoding
- (NSString *)urlEncodeString;
// decode a url encoded string
- (NSString *)urlDecodeString;
@end