一些命名规则:
比如要实现一个title的需求,可能需要一个titleLabel,一个titleHighLightView,一个titleBadgeView,对于这样的情况,如果你写成labelTitle, viewTitle, badgeTitle是不是很蠢?
把表达相同功能的控件,放在一起命名
例如上面说的。实现一个标题栏
UILabel *titleLabel;
UIImageView *titleIconImageView;
UIView *titleView;
UIButton *titleButton;
最好是这样命名:
UIlabel *[领名]+[作用名]+Label;
对于notification的命名规则
[Name of associated class] + [Did | Will] + [UniquePartOfName] + Notification
苹果的文章:Objective-C Runtime 运行时之三:方法与消息
1.在一些类的alloc方法或者工厂方法里面返回的时候不应该是返回"id",而应该写"instancetype"。
//错误写法
//- (id)initWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
//应该写成
- (instancetype)initWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
+ (instancetype)userWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
文档原文:In your code, replace occurrences of id as a return value with instancetype where appropriate. This is typically the case for init methods and class factory methods.
2.尽量使用@property;
3.使用NS_ENUM and NS_OPTIONS
- 把 [NSNumber numberWithInt:3] 写成 @3
- 把 [dictionary setObject:@3 forKey:key] 写成 dictionary[key] = @3