1-多用字面量语法,少用与之等价的方法
使用字面量语法,可以缩减代码长度,使其更为易读,⚠️使用字面量创建的对象都是不可变的,经常用到的有: NSString,NSNumber,NSArray,NSDictionary。eg:
// OC方法创建
NSNumber *testNumber = [[NSNumber alloc]initWithInt:1];
NSArray *testArray = [[NSArray alloc]initWithObjects:@"测试1",@"测试2", nil];
NSDictionary *testDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"obj1",@"key1",@"obj2",@"key2", nil];
// 字面量方法创建
NSNumber *testNumber2 = @1;
NSArray *testArray2 = @[@"测试1",@"测试2"];
NSDictionary *testDic2 = @{@"key1":@"obj1",@"key2":@"obj2"};
// 字面量方式修改可变数组和字典
NSMutableArray *testArrays = [[NSMutableArray alloc]initWithObjects:@"测试1",@"测试2", nil];
NSMutableDictionary *testDics = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"obj1",@"key1",@"obj2",@"key2", nil];
testArrays[0] = @"替换1";
testDics[@"key1"] = @"替换1";
注意⚠️:用字面量语法创建数组时要注意,若数组或者字典元素中有nil,则会抛出异常。
2-用枚举表示状态、选项、状态码,这样操作起来对内容更清晰
typedef NS_ENUM(NSInteger, ZKKAnimationTransition) {
ZKKAnimationTransitionFlashGo =0,//正序
ZKKAnimationTransitionFlashBack,//倒叙
ZKKAnimationTransitionCubeLeft ,//立方体向左
ZKKAnimationTransitionCubeUp ,//立方体向上
ZKKAnimationTransitionCubeRight ,//立方体向右
ZKKAnimationTransitionCubeDown ,//立方体向下
};
@property(assign,nonatomic)ZKKAnimationTransition animationTransitionStyle;