int multiplier = 7;
int (^myBlock)(int) = ^(int num) { return num * multiplier ;};
Block类型:int (^)(int)
Block变量:my block
Block值:^(int sum){return num * 7};即^返回值类型(参数列表){函数体} 其中返回值类型可以省略
typedef int (^Myblock)(NSString* str);
typedef NSString* (^Block)(int);
typedef int (^Myblock1)(NSString* str,NSString*str1);
int main(int argc, const char * argv[])
{
// __block int number = 7;
// int (^myBlock)(int) = ^ (int num) {
// number++;
// return number*num;
// };
// int num1 = myBlock(12);
// NSLog(@"%d",num1);
//
//
// NSString *str = @"234";
// int (^bolck)(NSString*) = ^(NSString* str){
// return [str intValue];//intValue是将(数字)字符串转化为数字
//
// };
// NSLog(@"%d",bolck(str));
// Myblock block2 = ^(NSString* str){
// return [str intValue];//intValue是将(数字)字符串转化为数字
//
// };
// NSLog(@"%d",block2(str));
// //无参无返回值
// void (^block1)() = ^{
// NSLog(@"hello word") ;
// };
// block1();
// Block block = ^(int num){
// return [[NSString alloc]initWithFormat:@"%d",num ];
// };
// Block block1 = ^(int num){
// return [[NSString alloc]initWithFormat:@"%d",num ];
// };
// Block block2 = ^(int num){
// return [NSString stringWithFormat:@"%d",num];
// };
// NSLog(@"%@\n%@",block(1245476),block2(48762368));
// NSString *str = @"ewtgu";
// NSString *str1 = @"masdlkf";
// Myblock1 block = ^(NSString *str,NSString *str1){
// return (int)([str length]+[str1 length]);
// };
NSArray *array = [[NSArray alloc]initWithObjects:@"123",@"dsf",@"345",@"vbj",@"457",@"dbnkm",@"789",@"fgj",@"56",@"fgj",@"5t67",@"dfghn",@"bnm",@"7jhgj", nil];
NSComparator sortBlock = ^(id str1,id str2){
return [str1 compare:str2 ];
};
NSArray *newArray = [array sortedArrayUsingComparator:sortBlock];
NSArray *newArray1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"%@",newArray1);
// Students *stu1 = [[Students alloc]initWithName:@"zhangsan" andNumber:@"000000000001" andAge:32 andScore:234];
// Students *stu2 = [[Students alloc]initWithName:@"lisi" andNumber:@"000000000002" andAge:23 andScore:456];
// Students *stu3 = [[Students alloc]initWithName:@"wanger" andNumber:@"000000000003" andAge:45 andScore:4567];
// Students *stu4 = [[Students alloc]initWithName:@"mazi" andNumber:@"000000000003" andAge:67 andScore:4567];
// Students *stu5 = [[Students alloc]initWithName:@"gaofushuai" andNumber:@"000000000005" andAge:22 andScore:56];
// Students *stu6 = [[Students alloc]initWithName:@"zhouxun" andNumber:@"000000000006" andAge:55 andScore:89.5];
// NSArray *array = [[NSArray alloc]initWithObjects:stu1,stu2,stu3,stu4,stu5,stu6, nil];
// NSArray *newArray = [array sortedArrayUsingSelector:@selector(compareStudentWithName:)];
// NSArray *newArray2 = [array sortedArrayUsingSelector:@selector(compareStudentWithAgeD:)];
// NSArray *newArray3 = [array sortedArrayUsingSelector:@selector(compareStudentWithAgeL:)];
// NSLog(@"%@",newArray);
// NSLog(@"%@",newArray2);
// NSLog(@"%@",newArray3);
// //姓名从小到大
// NSArray *newArray4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// Students *obj11 = (Students *)obj1;
// Students *obj12 = (Students *)obj2;
// return [[obj11 name] compare:[obj12 name]];
// }];
// NSLog(@"%@",newArray4);
// //姓名从小到大
// NSArray *newArray5 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// Students *obj11 = (Students *)obj1;
// Students *obj12 = (Students *)obj2;
// return -[[obj11 name] compare:[obj12 name]];
// }];
// NSLog(@"%@",newArray5);
// //年纪从小到大
// NSArray *newArray6 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// Students *obj11 = (Students *)obj1;
// Students *obj12 = (Students *)obj2;
// return [obj11 age] > [obj12 age];
// }];
// NSLog(@"%@",newArray6);
// //年纪从大到小
// NSArray *newArray7 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// Students *obj11 = (Students *)obj1;
// Students *obj12 = (Students *)obj2;
// return [obj11 age] < [obj12 age];
// }];
// NSLog(@"%@",newArray7);
// //分数从小到大
// NSArray *newArray8 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// return [obj1 score] > [obj2 score];
// }];
// NSLog(@"%@",newArray8);
// //分数从大到小
// NSArray *newArray9= [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// return [obj1 score] < [obj2 score];
// }];
// NSLog(@"%@",newArray9);
// //学号从小到大
// NSArray *newArray10 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// return [[obj1 number] compare:[obj2 number]];
// }];
// NSLog(@"%@",newArray10);
// //学号从小到大
// NSArray *newArray11 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// return -[[obj1 number] compare:[obj2 number]];
// }];
// NSLog(@"%@",newArray11);
2.Literals(字面量),是一种简易表示值的方法
// NSString *str = @"dsgmk";
// NSArray *array = @[@"12",@"ewf",@"dg"];
// NSString *str1 = array[1];
// NSDictionary *dic = @{@"1": @"胸大",@"2":@"熊二"};
// NSString *str2 = dic[@"1"];
// NSLog(@"str = %@,str1 = %@,str2 = %@",str ,str1,str2);