swift中闭包和OC的block的对比

//

//  ViewController.swift

//  swift的函数和闭包

//

//  Created by Ninesday on 16/6/23.

//  Copyright © 2016年 Ninesday. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        //swift中函数调用时候第一个参数可以省略

//        print(sum(10, y: 20))

//        print(sum2(num1: 10, num2: 21))

        

        

        //demo2()

        //demo3()

        //demo4()

        demo5()

    }

    

    //函数

    func sum(x:Int, y:Int) ->Int {

        return x + y

    }

    //外部参数 不会影响函数内部代码的执行  方便阅读

    //外部参数在闭包中很重要  

    //没有返回值的情况

    //1.什么都不写   2.Void     3,.()

    func sum2(num1 x:Int, num2 y:Int) -> Int {

        return x + y

    }

    

    //MARK:-闭包的定义

    //先看block,一组预先准备好的代码 在需要的时候执行 可以当参数传递 出现self注意循环引用

    //在swift中 函数本身就可以做参数传递

    //函数作为参数的例子

    

    //定义闭包

 /// 闭包的所有代码[参数 返回值 执行代码]都放在{}中

 //in  是用来区分函数定义和执行代码

    

    func demo2() {

        let sumFunc = { (num1 x:Int, num2 y: Int) -> Int

            in

            return x + y

        }

   //     print(sumFunc(2,3))

    print(sumFunc (num1: 1, num2: 2))

    }

    

    //闭包的格式

    //{(外部参数  形参列表 -> 返回值 in // 代码执行}

//    简单的闭包 没有参数和返回值 和in

    func demo3() {

        let  demo = {

            print("hello")

        }

        demo()

    }

    

    //MARK:-闭包的演练

    func demo4() {

    //block 从使用场景 --通常在异步加载网络数据 完成回调

        

        func loadData(finish:()->()) {

            dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in

                print("耗时操作(NSThread.currentThread())")

                //里面通常加载数据

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

                    print("完成(NSThread.currentThread())")

                    //执行finish回调

                    finish()

                })

              

            }

        }

        loadData { () -> () in

            print("回调的代码")

        }

     

    }

    

    func demo5() {

        //blcok的使用场景 --有参数的传递

        func loaddata(finished:(html:String)->()) {

            dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in

                print("耗时操作(NSThread.currentThread())")

                //里面加载数据

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

                    print("完成(NSThread.currentThread())")

                    finished(html: "<html><html>")

                })

            }

        }

        

        loaddata { (string) -> () in

            print("完成回调代码")

        }

    }

    

}

//OC中的block的

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self laodData:^(NSString *html) {

        NSLog(@"回调代码 %@",html);

    }];

}

-(void)laodData:(void (^)(NSString *html)) finished {

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSLog(@"耗时操作");

        dispatch_async(dispatch_get_main_queue(), ^{

            NSLog(@"准备主线陈的回调");

            finished(@"<html>");

        });

    });

}

   

原文地址:https://www.cnblogs.com/Ninesday/p/5611701.html