OCdemo-06 Block

//
//  main.m
//  OCdemo-06
//
//  Created by lanou3g on 15/10/14.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"




void sayHi(){
    NSLog(@"hello");
}
int maxValue(int a, int b){
    return a > b ? a : b;
}


int number2 = 20; //定义一个全局变量


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        

#pragma mark 函数指针
        //调用函数
        sayHi();
        int max = maxValue(2,3);
        NSLog(@"max = %d",max);
        //定义一个函数指针,指向 sayHi
        void (*p)() = NULL;
        p = sayHi;
        
        //定义一个函数指针,指向 maxValue
        int (*q)(int , int) = NULL;
        q = maxValue;
        
        //通过函数指针 调用函数
        p();
        int max1 = q(4,5);
        NSLog(@"max1 = %d",max1);
        
        
#pragma mark block
        // sayHiBlock 是一个block 变量名
        // block 的类型: void (^)()
        // ^  只是用来表示这是一个block 对象,和函数指针中的 * 一样,只是一个标示作用
        
        void (^sayHiBlock)()  = ^void (){
            NSLog(@"hello,我是一个block");
        };
        sayHiBlock();
        
        //定义匿名函数时,返回值类型可以省略
        //定义一个block,求两个数的最大值
        int (^maxBlock)(int , int) = ^(int a,int b){
            return a > b ? a : b;
        };
        //调用
        int max2 = maxBlock(8,9);
        NSLog(@"max2 = %d",max2);
        
        //定义一个block,求两个数的和
        int (^sumBlock)(int , int) = ^(int a, int b){
            return a + b;
        };
        int sum = sumBlock(1,2);
        NSLog(@"a + b = %d", sum);

        //写一个 返回值为整型 参数为OC字符串(仅一个参数)的block,实现将字符串转换为整型的功能
        int (^changeStringToIntBlock)(NSString *) = ^(NSString *string){
            return [string intValue];
        };
        NSString *str = @"123";
        int n = changeStringToIntBlock(str);
        NSLog(@"%d",n);
        
        //定义一个block,返回值是NSString对象,参数是一个NSArray 对象,实现从数组中取出第三个元素的功能
        NSString *(^block)(NSArray *) = ^NSString *(NSArray *array){
            if ([array count] >= 3) {
                return [array objectAtIndex:2];
                //return array[2];
            }else{
                return @"";
            }
        };
        NSArray *array = @[@"stu1",@"stu2",@"stu3"];
        NSString *_result = block(array);
        NSLog(@"%@",_result);
        
        
#pragma mark Block的typedef
        typedef int (^MaxBlock)(int , int );
        MaxBlock maxblock = ^(int a ,int b){
            return a > b ? a : b;
        };
        NSLog(@"%d",maxblock(2,3));
        
        
        typedef int (^ChangeStringToIntBlock)(NSString *);
        ChangeStringToIntBlock change = ^(NSString *string){
            return [string intValue];
        };
        NSLog(@"%d",change(@"456"));
        
        
        
        /*
#pragma mark block与局部变量
        
        //如果想要在block 内部修改局部变量的值,需要加“__block”修饰
        
       __block int number = 10;
        int (^addBlock)(int) = ^(int a){
        
            int sum = a + number;  // block 内部可以使用局部变量
            
            sum += number2;   //全局变量可以在 block 内部使用
            number2++;        // 和 修改
            
            
            number++;              // 在 block 内部不能直接修改局部变量的值
            return sum;
        
        };
        NSLog(@"sum = %d",addBlock(5));
        //-------------------------------
        __block int c = 1;
        int (^sumBlock)(int, int) = ^(int a, int b){
            
            int sum1 = a + b;
            
            sum1 += c;
            
            return sum1;
        };
        NSLog(@"sum1 = %d",sumBlock(5,6));
         //-------------------------------
        
#pragma mark Block与数组排列
        NSArray *sortArray = @[@"dahuang",@"huangshang",@"xiaojie",@"yizhang",@"tongzhuo"];
        
//        sortArray = [sortArray sortedArrayUsingSelector:@selector(compare:)];
//        NSLog(@"%@",sortArray);
        
        sortArray = [sortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            
            return [obj1 compare:obj2];
        }];
        
        NSLog(@"%@",sortArray);
        
        //定义一个 block 变量 用来排序
        NSComparator sortedArray = ^NSComparisonResult(id obj1, id obj2){
            
            //降序排列
            return -[obj1 compare:obj2];
        };
        
        sortArray = [sortArray sortedArrayUsingComparator:sortedArray];
        NSLog(@"%@",sortArray);
        //-----------------------------------------------------------------
        
        //可变数组排序
        NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"haohao",@"xiaoqiangzi",@"xiaohuangzi",@"xiaoxingzi",@"yizhang" ,nil];
        
        NSComparator mSortBlock = ^NSComparisonResult(id obj1, id obj2){
            
            return [obj1 compare:obj2];
        };
        
        [mArray sortUsingComparator:mSortBlock];
        NSLog(@"%@",mArray);
        
        
        */
        
        Student *stu1 = [Student studentWithNum:@"3838438" name:@"D大黄" age:38];
        Student *stu2 = [Student studentWithNum:@"58" name:@"L老王吧" age:39];
        Student *stu3 = [Student studentWithNum:@"7" name:@"X小范" age:18];
        Student *stu4 = [Student studentWithNum:@"61" name:@"H皇上" age:80];
        Student *stu5 = [Student studentWithNum:@"59" name:@"C村长" age:25];
        
        NSMutableArray *studentArray = [NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5, nil];
        
        //按照学生的姓名排序
        [studentArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            Student *student1 = obj1;
            Student *student2 = obj2;
            
            return [[student1 getName] compare:[student2 getName]];
        }];
        //遍历数组
        for (Student *stu in studentArray) {
            NSLog(@"%@",stu);
        }
        
        //按照学生的年龄排序
        [studentArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            Student *student1 = obj1;
            Student *student2 = obj2;
            
            if ([student1 getAge] < [student2 getAge]) {
                return NSOrderedAscending;
            } else  if ([student1 getAge] > [student2 getAge]) {
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
            }
        }];
        //遍历数组
        for (Student *stu in studentArray) {
            NSLog(@"%@",stu);
        }
        
#pragma mark 字面量
        
        //使用字面量创建字符串对象
        NSString *string = @"字符串";
        //使用字面量创建数组对象
        NSArray *array2 = @[@1,@2,@3,@"haha"];
        //从数组中取出元素
        NSLog(@"%@",array2[3]);
        //使用字面量创建字典对象
        NSDictionary *dict = @{
                               @"name":@"dahuang",
                               @"gender":@"母",
                               @"hobby":@"

你可能感兴趣的:(OCdemo-06 Block)