首页 > 代码库 > 笑笑语法,语法糖 ---- 字面量
笑笑语法,语法糖 ---- 字面量
//1.快速创建NSNumber对象
NSNumber *number1 = @20; //@ + 常量
int a = 20;
NSNumber *number2 = @(a); //@ + (变量)
//2.快速创建将C语言的字符串常量转为OC的字符串对象
NSString *str1 = @"Frank";
//3.快速创建NSArry对象
NSArray *arr1 = @[@"aa", @"hu", @"ty", @"fd"];
//4.快速创建NSMutableArry对象
NSMutableArray *arr2 = [@[@"dg", @"ji", @"gt", @"fg"] mutableCopy];
//5.快速根据下标取出数组中的元素
NSString *str2 = arr1[0];
NSString *str3 = arr2[3];
//6.快速创建NSDictionary字典对象
NSDictionary *dic = @{@"name":@"Frank", @"age":@"19", @"gender":@"man"};
//7.快速创建NSMutableDictionary对象
NSMutableDictionary *dic1 = [@{@"name":@"Frank", @"age":@"19", @"gender":@"man"} mutableCopy];
//8.快速的根据key取出对应的value
NSString *str4 = dic[@"name"];
NSString *srt5 = dic1[@"age"];
2.将 NSNumber数据对象转化为基本数据类型
NSNumber *number1 = @20;
NSNumber *number2 =@19.9;
NSNumber *number3 = @NO;
NSNumber *number4 = @‘m‘;
int newNumber1 = [number1 intValue];
NSLog(@"%d", newNumber1);
float newNumber2 = [number2 floatValue];
NSLog(@"%.1f", newNumber2);
BOOL newNumber3 = [number3 boolValue];
NSLog(@"%d", newNumber3);
char newNumber4 = [number4 charValue];
NSLog(@"%c", newNumber4);
笑笑语法,语法糖 ---- 字面量