Block与数组排序,局部变量,字面量

//有参有返

        int(^myblock5)(int) = ^int(int lll){return lll * 8;};

        int k = myblock5(9);

        NSLog(@"k = %d",k);

        

        

        int(^myblock6)(int) = ^int(int lll){return (int)lll;};//'(int)'强制类型转化

        NSLog(@"%d",myblock6(0.618));

        

        

        

        

       //返回三个数之和的block


        int (^myblock9)(int,int,int) = ^int(int a,int b,int c){return a + b + c;};

        

        int sum = myblock9(1,2,3);

        NSLog(@"sum = %d",sum);

        

        int (^myblock8)(int) = ^int(int num){return num + 10;};

       int xx = myblock8(40);

        NSLog(@"xx = %d",xx);

        

        /*

         block类型    :    int(^)int

         block变量名  :     myfirstblock

         block     :     ^(int) (int num){return num * 7}

        

                      ^返回值类型(参数列表){函数体};其实返回值类型可以省略

         */

        

        

        

       //写一个返回值为整形,参数为NSString(仅一个参数)的block,把字符串转化为整形

       int (^myblock10)(NSString *) = ^int(NSString *str){

           

           //字符串转化为整形的方法

           return str.intValue;};

    

        int n = myblock10(@"123");

        NSLog(@"n = %d",n);

        

        

        /*********************Block使用***********************/

        

//使用typedef定义block

        typedef int(^easyblock) (int, int);

      //原类型:int(^)(int, int);

        //新类型:easyblock

        

        

        easyblock eablock = ^(int x,int y){

            return x + y;

        };

        NSLog(@"eablock = %d",eablock(2,3));

        

        /********************  Block局部变量  ***********************/

        

        

//         block块中可以访问外部的局部变量

       //如果需要在block内部修改局部变量的值,在局部变量定义时加上__block修饰

        

        

        float cc=3.14;

        

        void (^block2)(float)=^void(float x){

            //block可以访问外部的局部变量

            

            NSLog(@"%.2f",cc+x);

        };

        block2(1);

        //如果需要在block内修改局部变量的值,在局部变量定义是加上__block修饰...  block  前面有两个__

       

        

        

//         block块中可以访问全局变量

        

        int (^kkblock)(int) = ^int(int bm){

            //全局变量不需要__block修饰就可以去修改

            number2 =number2 + 512;

            return bm +=number2;

        };

        NSLog(@"kkblock = %d",kkblock(512));

        

        

  /********************  Block与数组排序  ***********************/

        

        //定义需要排序得到数组

        NSArray *nameArray = [NSArrayarrayWithObjects:@"zhangpengfei",@"qiaozipeng",@"zhaoyantao",@"zhongchenyang",@"kenan",nil];


//typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);


        //NSComparisonResult    :    block块的返回值类型(枚举类型)

        //NSComparator          :   经过typedef重新定义后的新的block块的名字

        //(id obj1,id obj2)     :   参数类型,两个都是id类型 id是代指任何类型)

        NSComparator sortblock = ^NSComparisonResult(id obj1,id obj2){

            return [obj1compare:obj2];

        };

        

//重新定义新的数组去接受排序好的数据,原数组不可变不能直接交换元素位置

        //sortedArrayUsingComparator 排序方法

        NSArray *sortArray = [nameArraysortedArrayUsingComparator:sortblock];

        

        NSLog(@"sortArray = %@",sortArray);

        

        //创建几个student对象

        Student *stu1 = [Studentwithname:@"xiaoming"

              andsex:@""

           andnumber:00001

             withage:16

           andheight:160.12];

        

        Student *stu2 = [Studentwithname:@"xiaogang"

                                          andsex:@""

                                       andnumber:00002

                                         withage:17

                                       andheight:160.12];

        

        

        Student *stu3 = [Studentwithname:@"xiaoli"

                                          andsex:@""

                                       andnumber:00003

                                         withage:16

                                       andheight:160.12];

        

        Student *stu4 = [Studentwithname:@"xiaohong"

                                   andsex:@""

                                andnumber:00004

                                  withage:16

                                andheight:155.12];

        

        Student *stu5 = [Studentwithname:@"xiaoya"

                                   andsex:@""

                                andnumber:00005

                                  withage:16

                                andheight:155.12];

        

        Student *stu6 = [Studentwithname:@"xiaowang"

                                   andsex:@""

                                andnumber:00006

                                  withage:16

                                andheight:155.12];

        

        NSMutableArray *students = [NSMutableArrayarrayWithObjects:stu1, stu2,stu3,stu4,stu5,stu6,nil];

        

        

        

//        [students sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

//            //(student *)是强制类型转换标识,obj1id类型,当我们需要使用时,可以强化为所需要的类型;

//            Student *stu1 = (Student *)obj1;

//            Student *stu2 = (Student *)obj2;

//            //升序排列

//            //如果第一个对象的名字小于第二个对象的名字,那么,按照升序排列;

//            if ([[stu1 name]compare:[stu2 name]] < 0) {

//                return NSOrderedAscending;

//            }

//            //降序排列

//            //如果第一个对象的名字大于第二个对象的名字,那么按照降序排列;

//            if ([[stu1 name]compare:[stu2 name]] > 0) {

//                return NSOrderedDescending;

//            }

//            //相等;

//            else  {

//                return NSOrderedSame;

//            }

//        }];

       //遍历数组,查看遍历后的内容;

//        for (Student *stu in students) {

//            NSLog(@"%@  %@  %ld  %ld",stu.name,stu.sex,stu.age,stu.stuNumber);

//        }

        

        

//        /*********

           [students sortUsingComparator:^NSComparisonResult(id obj1,id  obj2) {

           //(Student *)是强制类型转换的标志,obj1id类型,当我们需要使用的时候就可以强转为我们需要的类型

            Student *stu1 = (Student *)obj1;

            Student *stu2 = (Student *)obj2;

               if ([[stu1name]compare : [stu2name]] < 0) {

                   returnNSOrderedAscending;

               } elseif([[stu1name]compare : [stu2name]] > 0){

                   returnNSOrderedDescending;

            }else{

                returnNSOrderedSame;

            }


        }];

  //      ********/

       //遍历数组,查看排序后的内容

        for (Student *stuin students) {

            NSLog(@"%@",[stuname]);

                       }


        

         /********************  iOS字面量  ***********************/

       //字面量创建的对象是便利构造的,且是不可变的

        

        

        //NSString使用字面量

        NSString *str1 =@"ailw4bnpwiekluwevyblueibvtauv";

        

        NSString *str2 =@"oaubnlaealuybvlawu4btviy";

        

        NSLog(@"%@%@",str1,str2);

        

        

          //NSArray使用字面量创建     数组

        

        NSArray *leterArray = @[@"woiru",@"bvp",@"runp9"];

        

        //NSAArray使用字面量访问

        

        NSLog(@"%@",leterArray[0]);

         NSLog(@"%@",leterArray[1]);

        


        //NSAArray未使用字面量访问元素

        

        NSLog(@"%@",[leterArrayobjectAtIndex:0]) ;

         NSLog(@"%@",[leterArrayobjectAtIndex:1]) ;

        

        

        // NSDictionary使用字面量创建   字典

       //@""  :   @""  key  :  value

       //与字典直接创建不同,直接创建的时候value在左边,key在右边

        //使用字面量的时候key在左边,value在右边

        

        NSDictionary *dic = @{@"1":@"kuaebgv",@"iuegte":@"kjgry"};

        

       // NSDictionary使用字面量访问元素

        NSString *let = dic[@"1"];

        NSLog(@"let = %@",let);

        

       //未使用字面量

        NSString *let1 = [dicvalueForKey:@"1"];

        NSLog(@"let1 = %@",let1);

        

        

        /*

         

         下节预告

         

         NSDate     NSDateFormatter

         

         Category  Extension   Protocol

         */


定义的类:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值