block--閉包 的分析使用

1.object-C中的block

    1. 作用:保存一段代碼塊

  • 2.聲明
    block的寫法:

    block的寫法: 類型: 返回值類型(^block的名稱)(block的參數(shù))
    值: ^(參數(shù)列表) {
    // 執(zhí)行的代碼
    }
    //例子 
    int (^sumOfNumbers)(int a, int b) = ^(int a, int b) { return a + b; };
    

  • 3.定義
    block的定義有三種方式
    // block定義的三種方式

      // 方式一
          void(^block1)() = ^(){
      
      };
      // 方式二 如果沒有參數(shù),參數(shù)可以隱藏,如果有參數(shù),定義的時候必須寫參數(shù),而且必須要有參數(shù)變量名
      void(^block2)() = ^{
      
      };
      void(^block2_1)(int) = ^(int a){//這里的參數(shù)a不能省略,因為下面的代碼塊要用到這個參數(shù) 
          
      };
      void(^block2_2)(int a) = ^(int a){
          
      };
    
      // 方式三 block的返回是可以省略的,不管有沒有返回值都可以省略
      int (^block3)() = ^int{
          return 0;
      };
      int (^block3_1)() = ^{
          return 0;
      };
    

  • 4.類型

      // 2.block的定義:int(^)(NSString *)<----這就是block的聲明
      int(^block4)(NSString *) = ^(NSString *str){
          return 1;
      };
      
      block1();//block的調(diào)用
      //inLineBlock:快速創(chuàng)建一個block的快捷方式,如下
      <#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^(<#parameters#>) {
          <#statements#>
      };
    

2.block在開發(fā)中的使用場景

  • 1.場景一 (基本沒有實際意義)
    目的:在一個方法中定義了代碼塊,可以在另一個方法中去使用;此種方法和直接寫一個方法等價,沒多大意義。
    #import "ViewController.h"

      // BlockName:block類型別名
      typedef void(^BlockName)();
    
      @interface ViewController ()
    
      // block怎么聲明就怎么定義屬性
      // 方式一 : 直接用block聲明,block如何聲明就如何定義成屬性
      @property (nonatomic,strong) void(^block)();
      // 方式二 : 給block起別名,用別名作為類型
      @property (nonatomic,strong) BlockName block1;
    
      @end
    
      @implementation ViewController
    
      - (void)viewDidLoad {
          [super viewDidLoad];
          
          void(^block)() = ^{
              NSLog(@"直接用block聲明的block   調(diào)用了block");
          };
          
          void(^blockName)() = ^{
              NSLog(@"使用block起別名的方式   調(diào)用了block");
          };
          
          _block = block;
          _block1 = blockName;
    
      }
      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
          // 調(diào)用block
          _block1();
          _block();
      }
    
     打印輸出結(jié)果:
     1.使用block起別名的方式   調(diào)用了block
     2.直接用block聲明的block   調(diào)用了block
    

  • 2.場景二 (應(yīng)用最多)
    目的:在一個類中定義, 在另外一個類中去使用
    #import <Foundation/Foundation.h>

      @interface CellItem : NSObject
      @property (nonatomic, copy) NSString *title;
    
      @property (nonatomic, strong) void(^block)(); // 作為屬性保存到這個模型中
    
      + (instancetype)itemWithTitle:(NSString *)title;
      @end
      #import "CellItem.h"
    
      @implementation CellItem
    
      + (instancetype)itemWithTitle:(NSString *)title {
          CellItem *item = [[self alloc] init];
          item.title = title;
          return item;
      }
      @end
    

    #import "ViewController.h"
    #import "CellItem.h"

    @interface ViewController ()

    @property (nonatomic, strong) NSArray *itemArray;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 初始化tableView
        _myTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [self.view addSubview:_myTableView];
        
        CellItem *item1 = [CellItem itemWithTitle:@"打電話"];
        item1.block = ^{
            NSLog(@"調(diào)用打電話功能");
        };
        
        CellItem *item2 = [CellItem itemWithTitle:@"發(fā)短信"];
        item2.block = ^{
            NSLog(@"調(diào)用發(fā)短信功能");
        };

        CellItem *item3 = [CellItem itemWithTitle:@"發(fā)郵件"];
        item3.block = ^{
            NSLog(@"調(diào)用發(fā)郵件功能");
        };

        _itemArray = @[item1,item2,item3];
    }



    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _itemArray.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *ID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        }
        // 此處如果不用數(shù)組就必須使用大量的ifelse語句
        CellItem *item = self.itemArray[indexPath.row];
        cell.textLabel.text = item.title;
        return cell;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CellItem *item = self.itemArray[indexPath.row];
        // 此處如果不用數(shù)組就必須使用大量的ifelse語句
        if (item.block) {// 必須要判斷下block是否有值
            item.block();
        }  
    }
  • 3.場景三
    目的:使用block去傳值,可與delegate互換。(數(shù)據(jù)的逆向傳遞)

     #pragma mark
     這里ModaViewController想把一個字符串傳遞給ViewController,
     于是ModaViewController就需要拿到ViewController,
     那么就要在ModaViewController中寫一個代理,
     讓ViewController去實現(xiàn)這個代理;
    
      #import <UIKit/UIKit.h>
    
      //@class ModaViewController;
      //
      //@protocol ModaViewControllerDelegate <NSObject>
      //@optional
      //// 設(shè)計需要實現(xiàn)的方法: 想要代理做什么事情就怎么去設(shè)計
      //// 什么時候?qū)崿F(xiàn)代理?---> 這里就簡單的定義為當(dāng)點擊屏幕的時候就將value值傳遞過去
      //- (void)modaViewController:(ModaViewController *)vc sendValue:(NSString *)value;
      //@end
    
      @interface ModaViewController : UIViewController
      // 用block替換下面的代理--哪里用代理,哪里就可以用block去替換
      @property (nonatomic, strong) void(^block)(NSString *value); // <---value是block的傳入?yún)?shù)
    
      //@property (nonatomic, weak) id<ModaViewControllerDelegate> myDelegate;
      @end
      --------------------------------------------------------------
      #import "ModaViewController.h"
    
      @interface ModaViewController ()
    
      @end
    
      @implementation ModaViewController
    
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
          self.view.backgroundColor = [UIColor blueColor];
      }
    
      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
          // 傳值給ViewController
      //    if ([_myDelegate respondsToSelector:@selector(modaViewController:sendValue:)]) { // 查看這個代理是否有需要實現(xiàn)的方法
      //        // 如果有需要實現(xiàn)的方法就讓代理直接去實現(xiàn)
      //        [_myDelegate modaViewController:self sendValue:@"123456"];
      //    }
          if (_block) {
              _block(@"123456");
          }
          
      }
      @end
        --------------------------------------------------------------
      #import "ViewController.h"
      #import "ModaViewController.h"
    
      @interface ViewController () //<ModaViewControllerDelegate>
    
      @end
      
      @implementation ViewController
    
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      }
    
      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
          
          // 推出控制器
          ModaViewController *modaVC = [ModaViewController new];
          [self presentViewController:modaVC animated:YES completion:nil];
          
          // 在這里需要ViewController控制器拿到ModaViewController的代理方法,代理并實現(xiàn)拿到的代理方法
      //    modaVC.myDelegate = self;
          
          modaVC.block = ^(NSString *value) {
              NSLog(@"打印block傳過來的值  == %@", value);
          };
          
      }
    
      #pragma mark ModaViewController---myDelegate
      //- (void)modaViewController:(ModaViewController *)vc sendValue:(NSString *)value {
      //    NSLog(@"打印代理傳過來的值  == %@", value);
      //}
      @end
    
屏幕快照 2017-10-17 下午3.12.06.png
屏幕快照 2017-10-17 下午3.12.31.png
屏幕快照 2017-10-17 下午3.13.13.png
  • 4.block作為參數(shù)進(jìn)行延時回調(diào)
    定義網(wǎng)絡(luò)請求的類

    @interface HttpTool : NSObject 
    -(void)loadRequest:(void (^)())callBackBlock; 
    @end 
    @implementation HttpTool 
    -(void)loadRequest:(void (^)())callBackBlock {         
         dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
              NSLog(@"異步延時請求操作在這里,加載網(wǎng)絡(luò)數(shù)據(jù):%@", [NSThread currentThread]);
              dispatch_async(dispatch_get_main_queue(), ^{
                        callBackBlock(); 
               }); 
         }); 
    }          
    @end
    

    進(jìn)行網(wǎng)絡(luò)請求,請求到數(shù)據(jù)后利用block進(jìn)行回調(diào)

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
      [self.httpTool loadRequest:^{
          NSLog(@"主線程中,將數(shù)據(jù)回調(diào).%@", [NSThread currentThread]);
      }];
    }
    

Swift中的閉包

<1>. 閉包寫法總結(jié):

類型:(形參列表)->(返回值)
技巧:初學(xué)者定義閉包類型,直接寫()->().再填充參數(shù)和返回值

值:
{
    (形參) -> 返回值類型 in
    // 執(zhí)行代碼
}

let b = { (parm : Int) -> (Int) in 
   print(parm)
}

//調(diào)用
b(100)

<2>.閉包的簡寫

如果閉包沒有參數(shù),沒有返回值,in和in之前的內(nèi)容可以省略

  httpTool.loadRequest({
      print("回到主線程", NSThread.currentThread());
  })

尾隨閉包寫法:
    如果閉包是函數(shù)的最后一個參數(shù),則可以將閉包寫在()后面

    如果函數(shù)只有一個參數(shù),并且這個參數(shù)是閉包,那么()可以不寫

    httpTool.loadRequest() {
        print("回到主線程", NSThread.currentThread());
    }

    // 開發(fā)中建議該寫法
    httpTool.loadRequest {
        print("回到主線程", NSThread.currentThread());
    }

<3>.使用閉包代替block,閉包作為參數(shù)進(jìn)行延時回調(diào)

定義網(wǎng)絡(luò)請求的類

class HttpTool: NSObject {
  func loadRequest(callBack : ()->()){
      dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
          print("加載數(shù)據(jù)", [NSThread.currentThread()])

           dispatch_async(dispatch_get_main_queue(), { () -> Void in
              callBack()
           })
      }
  }
}

進(jìn)行網(wǎng)絡(luò)請求,請求到數(shù)據(jù)后利用閉包進(jìn)行回調(diào)

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
      // 網(wǎng)絡(luò)請求
      httpTool.loadRequest ({ () -> () in
          print("回到主線程", NSThread.currentThread());
      })
  }

<3>.實例二,閉包的回調(diào)傳值

//[weak self]:解決循環(huán)引用導(dǎo)致的內(nèi)存泄露
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    delayMethod {[weak self] (re) ->() in
        print("$$$$$$$$$$$$$$$$$:\(re)%%%%%%%%%%%\(String(describing: self))")
    }
    delayMethod(comletion: {[weak self] (re)->() in
        print("********:\(re)*************\(String(describing: self))")
    })
}

//@escaping:逃逸閉包。它的定義非常簡單而且易于理解。如果一個閉包被作為一個參數(shù)傳遞給一個函數(shù),并且在函數(shù)return之后才被喚起執(zhí)行,那么這個閉包是逃逸閉包。
func delayMethod(comletion: @escaping (_ results: String,_ resultss:String) -> ()) ->(){
    //開啟一個全局異步子線程
    DispatchQueue.global().async {
        Thread.sleep(forTimeInterval: 2.0)
        //回調(diào)到主線程
        DispatchQueue.main.async(execute: {
            print("主線程更新 UI \(Thread.current)")
            comletion("qwertyui","asdf")
        })
    }
}

<4>.閉包進(jìn)行兩個界面的傳值

我們要實現(xiàn)點擊第二個界面后,關(guān)掉第二個界面,并且傳值給第一個界面
<1>.首先在第二個界面聲明閉包進(jìn)行操作

class NewViewController: UIViewController {
  //聲明閉包
  typealias lewisCloser = (_ paramOne : String? ) -> ()
  //定義個變量 類型就是上面聲明的閉包
  var customeCloser: lewisCloser?
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      if(customeCloser != nil) {
          customeCloser!("要發(fā)給第一個界面的值")
      }
      self.dismiss(animated: true, completion: nil)
  }
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
  }
}

<2>.在第一個界面實現(xiàn)閉包,取得要穿的值

let vc = NewViewController()
//實現(xiàn)閉包
vc.customeCloser = {(cusValue) -> () in
    //cusValue就是傳過來的值
    print("^^^^^^^^^^^^^^^^^^^^^:\(cusValue!)")
}
self.present(vc, animated: true, completion: nil)

關(guān)于Swift許多部分我自己也是一知半解,這篇文章完全是為自學(xué)加深記憶。如有發(fā)現(xiàn)錯誤煩請指正~~~
閉包部分完全抄襲:LewisZhu,抱歉?。。?大家也可移步至下方原著鏈接??
作者:LewisZhu
鏈接:http://www.lxweimin.com/p/1457a4894ec7

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評論 6 541
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,471評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內(nèi)容