开发过程中大家是不是都有自己的代码习惯,啪啪啪,键盘一敲,帅气十足,今天就讲一下语法糖,个中原因很简单,懒得看到一堆长长的代码(实际为了代码简练),在手起键盘落的时候语法糖是我经常用得到的有效提升代码逼格的语法.现在整理下语法糖(虽然简单,大家也要养成做笔记的好习惯),同时文章也会不断的完善。
**语法糖(Syntactic sugar)
也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。
OC语法糖
基础@[],@{}用法在NSArray,NSDictionary,NSNumber使用
"重点" @()使用
"重点" 语法糖在UI中的使用方法
常用基础使用部分@[],@{}这两部分的用法
1.一般数组的初始化和访问数组元素是这样的在之前的博客中我是这样初始化NSArray的:
//NSArray的便利初始化
NSArray *array1 = [[NSArray alloc] initWithObjects:@"aaa", @"bbb", @"ccc", nil];
//NSArray的便利构造器
NSArray *array2 = [NSArray arrayWithObjects:@"111", @"222", @"333", nil];
获取数组的元素
//获取相应索引的元素
id element = [array1 objectAtIndex:0];
NSLog(@"array1_count = %d, array[0] = %@", count, element);
简化后的数组初始化和访问的做法如下
//NSArray的定义
NSArray *array = @[@"lu", @"da", @"shi", @YES, @123];
int count = (int)[array count];
for (int i = 0; i < count; i++)
{
NSLog(@"%@", array[i]);
}
2.对字典(NSDictionary)的简化也引用我之前博客中得一段代码吧
//不可变字典的初始化
NSDictionary *dictionay = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1",
@"value2", @"key2", nil];
id value = [dictionay objectForKey:@"key1"];
NSLog(@"key1 => %@", value);
我们还可以这样做
//NSDictionary的定义简化
NSDictionary *dictionary = @{
@"key0" : @"value0",
@"key1" : @"value1",
@"key2" : @"value2"
};
NSLog(@"key2 => %@", dictionary[@"key2"]);
3.对NSNumber简化
我们可以这样做
把基本类型包装成对象的便利构造函数
-(id) initWithChar : (char) value;
-(id) initWithInt : (int) value;
-(id) initWithFloat : (float) value;
-(id) initWithBool: (BOOL) value;
把基本数据类型包装成对象的便利构造器
+(id) numberWithChar : (char) value;
+(id) numberWithInt : (int) value;
+(id) numberWithFloat : (float) value;
+(id) numberWithBool : (BOOL) value;
我们也可以这样做,说明:在char转换为NSNumber是存的是ASCII码的形式,c输出为97
//NSNumber的简化
NSNumber *a = @123;
NSNumber *b = @11.2;
NSNumber *c = @('a');
NSLog(@"a = %@, b = %@, c = %@", a, b, c);
针对以上部分的简单练习
//NSNumber的语法糖
NSNumber *intNumber = [NSNumber numberWithInt:100];
NSNumber *intNumber2 = @100;
//不可变字符串的语法糖
NSString *string = @"hanjunqiang";
//可变字符串的语法糖
NSMutableString *mString = @"大爱中华".mutableCopy;//后缀不能丢
//不可变数组的语法糖
NSArray *array = @[@"1",@"2",@"3",@"4"];
NSLog(@"%@",array);
//访问数组元素的语法糖
NSLog(@"%@",array[1]);
//可变数组的语法糖
NSMutableArray *mArray = @[@"1",@"2",@"3",@"4"].mutableCopy;
//字典的语法糖
//字典对象[key值]取出对应的value值
NSDictionary *dict = @{@"a":@"1",@"b":@"2"};//key值在冒号前,value值在冒号后
NSLog(@"%@",dict);
NSLog(@"%@",dict[@"a"]);
//可变字典可以赋值和修改值
NSMutableDictionary *mDic = @{@"a":@"1",@"b":@"2"}.mutableCopy;
mDic[@"a"]=@"100";
NSLog(@"%@",mDic[@"a"]);
UI使用部分
拿UIImageView来做例子,原来创建对象和下面一致
self.imageView = [[UIImageView alloc] init];
self.imageView.backgroundColor = [UIColor redColor];
self.imageView.image = [UIImage imageNamed:@
"12345"
];
self.imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:self.imageView];
重点来了这个就是语法糖在UI中创建对象的使用,瞬间感觉很高大上~
self.imageView = ({
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
imageView.image = [UIImage imageNamed:@
"12345"
];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
imageView;
});
转自"香蕉大大"