@xifenglang-33250
2018-12-03T17:36:49.000000Z
字数 6458
阅读 726
森特网
例子:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.api.queryName = @"";
[self loadData];
}
- (void) didSelectedRecentWithRow:(NSInteger)row {
self.api.docType = row * 10;
[self.tableView.mj_header beginRefreshing];
}
方法命名中使用空格的标准写法
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
[self.tableView.mj_header beginRefreshing];
}
例子:
@property (nonatomic, strong) NSArray <HTRegionModel *> *streets;
@property (nonatomic, assign) NSInteger index0;
@property (nonatomic, assign) NSInteger index1;
@property (nonatomic, assign) NSInteger index2;
@property (nonatomic, assign) NSInteger index3;
- (void)setStreets:(NSArray<HTRegionModel *> *)streets
{
_streets = streets;
if (_streets.count > 0) {
[self addBtnAndTableView];
[self moveLine];
self.lastBtn.selected = NO;
UITableView *tableView = (UITableView *)self.areaScrollView.subviews[self.lastBtn.tag];
[tableView reloadData];
}else {
[self moveLine];
self.lastBtn.selected = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hidenAreaView];
if (self.delegate) {
[self.delegate selectedIndexProvice:_index0 city:_index1 district:_index2 street:-1];
}
});
}
}
1、方法名尾部不应在“ {”前面换行,而是“ {”后换行,并且"{"前有个空格
2、声明NSArray、NSDictionary、NSString等不可变容器类型属性,应用copy修饰
3、重写NSArray、NSDictionary、NSString等不可变容器类型属性,setter里面应该赋值copy后的值,ARC声明block属性建议用copy,尽可能跟MRC写法逻辑保持一致。
4、在Block实现中调用成员变量_index0、_index1、_index2编译时会有警告,应改成self.proviceIndex
5、声明成员变量避免使用123等数字,可读性差,命名应追求顾名思义,适当的添加注释。index0应改成proviceIndex
修改后
@property (nonatomic, copy) NSArray <HTRegionModel *> *streets;
@property (nonatomic, assign) NSUInteger proviceIndex;
- (void)setStreets:(NSArray<HTRegionModel *> *)streets {
_streets = streets.copy;
if (streets.count > 0) {
[self addBtnAndTableView];
[self moveLine];
self.lastBtn.selected = NO;
UITableView *tableView = (UITableView *)self.areaScrollView.subviews[self.lastBtn.tag];
[tableView reloadData];
} else {
[self moveLine];
self.lastBtn.selected = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hidesAreaView];
if (self.delegate) {
[self.delegate selectedIndexProvice:self.proviceIndex city:self.cityIndex district:self.districtIndex street:-1];
}
});
}
}
其中涉及到的协议方法还可以再改
@protocol HTSelectedAddrViewDelegate
/// 选择后的下标
- (void) selectedIndexProvice:(NSInteger)index0 city:(NSInteger)index1 district:(NSInteger)index2 street:(NSInteger)index3;
@end
业务处理层需要的是直接可用的结果(HTRegionModel),而不是拿到下标去查数据。
应改成
@protocol HTSelectedAddrViewDelegate
- (void)didSelectedProvice:(HTRegionModel *)provice city:(HTRegionModel *)city district:(HTRegionModel *)district street:(HTRegionModel *)street;
/// 或者
- (void)addressSelectorView:(HTSelectedAddrView *)selectorView didSelectedProvice:(HTRegionModel *)provice city:(HTRegionModel *)city district:(HTRegionModel *)district street:(HTRegionModel *)street;
@end
渲染透明色和layer.cornerRadius倒圆角会明显降低FPS,初始化View应当设置背景色,XIB亦然,列表中出现圆角view尽量用贝塞尔曲线绘制图片。
[[UILabel alloc] init]
[[UIImageView alloc] init];
...
修改前:
NSInteger docType = [stockOutModel.docType integerValue];
if (docType == 0) {
self.stateLabel.text= @"待处理";
}else if (docType == 10) {
self.stateLabel.text= @"待拣货";
}else if (docType == 20) {
self.stateLabel.text= @"复核完成";
}else if (docType == 30) {
self.stateLabel.text= @"已发运";
}else if (docType == 40) {
self.stateLabel.text= @"已取消";
}else if (docType == 50) {
self.stateLabel.text= @"已关闭";
}else {
self.stateLabel.text= @"未知";
}
修改后:
优势:
1、方便改文案,不用多个地方改,容易出BUG
2、复用代码,减少垃圾代码
3、提炼更专一的函数,职能细分,方便重构
*.h
/** 根据状态码获取出库状态描述 */
UIKIT_EXTERN NSString * ERPStockOutStatusNameWith(NSUInteger status);
*.m
NSString * ERPStockOutStatusNameWith(NSUInteger status) {
switch (status) {
case 0:return @"待处理";
case 10:return @"待拣货";
case 20:return @"复核完成";
case 30:return @"已发运";
case 40:return @"已取消";
case 50:return @"已关闭";
default:return [NSString stringWithFormat:@"未知状态%ld",status];
}
}
其它.m
NSInteger docType = [stockOutModel.docType integerValue];
self.stateLabel.text = ERPStockOutStatusNameWith(docType);
修改前:
@protocol ERPStockReportItemViewDelegate <NSObject>
- (void) didItemWithRow:(NSInteger)row;
@end
@interface ERPStockReportItemView : UIView
- (instancetype)initWithTitles:(NSArray *)titles;
@property (nonatomic, weak) id <ERPStockReportItemViewDelegate> delegate;
@end
修改后
@protocol ERPStockReportItemViewDelegate <NSObject>
/// 通过方法名不能看懂方法的作用时,就应该修改命名或者补加注释
/// 以stockReportItemView开头,主要是为了表明协议方法的调用者,在实现方法的类可以明显的区分。仿UITableViewDelegate
- (void)stockReportItemView:(ERPStockReportItemView *)itemView didSelectedItemAtRow:(NSInteger)row;
@end
@interface ERPStockReportItemView : UIView
// NS_UNAVAILABLE会禁用外部调用实例化,NS_DESIGNATED_INITIALIZER 即指定的实例化方法
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
- (instancetype)initWithTitles:(NSArray *)titles NS_DESIGNATED_INITIALIZER;
@property (nonatomic, weak) id <ERPStockReportItemViewDelegate> delegate;
@end
案例
ERPStockTransferViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(freshNotification) name:stockAllotNotifacation object:nil];
}
缺点:在viewDidAppear
中多次注册监听,.m文件里面没有移除监听的代码
使用KVO时一定要移除监听,Notification大多数情况不移除监听不会闪退,但不代表绝对安全,而应本着谁添加谁移除的原则写相关代码,这块的关注点是代码习惯和代码质量。
修改方向:
1. 在viewDidLoad
中注册一次监听,在控制器生命周期将结束时移除监听,比如以下2个方法
- (void)stw_viewControllerWillDismissFromStack {}
- (void)dealloc {}
如果在viewWillAppear、viewDidAppear
添加监听,就应该在viewWillDisappear、viewDidDisappear
移除监听,监听和结束监听的时机应该是对应的。
将KVO或者Notification做成自动化移除
参考:
1. objc china的一篇译文《Lighter View Controllers》
2. iOS 开发中的 Self-Manager 模式
尽量将控制器的代码控制在500行以内,甚至300行以内,避免重复写无营养的代码。有个同行说过一句话:把垃圾代码放在一起
@property (nonatomic, strong) NSArray*storeExportDetailAppResDtos;
修改后:
1、NSArray*类型,strong换成copy;
2、storeExportDetailAppResDtos命名过长,去掉无意义的字段,改成storeExportDetail,这个地方甚至涉及到字段的前后端分离。
3、声明Array的元素类型
@property (nonatomic, copy) NSArray <ERPStockOutGoodModel *> *storeExportDetail;