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

         */


定義的類:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章