一.swift中闭包

swift中的闭包格式

1
2
3
4

{ (parameters) -> return type in
    statements
}

1. swift block, without any arguments and without return value

1
2
3
4
5
6
7
8
9
 typealias funcBlockA = () -> ()
 typealias funcBlockAA = () -> Void
 
var _funcBlockA:funcBlockA = {
    
    () -> Void in
    
    println("***block without any arguments and without return value")
}   

2.swift block with arguments and return value

1
2
3
4
5
6
7
var _funcBlockB:funcBlockB = {
    (stringA:String, stringB:String) ->String in
    
    println("***swift block with arguments and without any return value")
    
    return (stringA + stringB)
}  

3.swift block with arguments and return value as function

1
2
3
4
5
6
7
8

var _funcBlockC:funcBlockC = {
    
    (stringA:String, stringB:String) ->(String)->() in
    
    blockC("***wift block with arguments and  return value:\(stringA) and \(stringB)")
    return blockC
}

4.swift block as function parameter and return value as function with parameter and return value

1
2
3
4
5
6
7

var _funcBlockD:funcBlockD = {
    (stringA:String, stringB:String) ->(String)->(String) in
    
    blockD("***swift block as function parameter:\(stringA) and \(stringB)")
    return blockD
}

二.示例

1.示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

func blockC(name:String)  {
    
    println(" blockC= (\(name))")
}

func blockD(name:String)->String  {
    var result:String = "blockD = " + name
    
    println(result)
    
    return result;
}

class QBlock{
    
    //swift block, without any arguments and without return value
    typealias funcBlockA = () -> ()
    typealias funcBlockAA = () -> Void
    //swift block with arguments and return value
    typealias funcBlockB = (String, String) -> String
    //swift block with arguments and  return value as function
    typealias funcBlockC = (String, String) -> (String)->()
    //swift block as function parameter and  return value as function with parameter and return value
    typealias funcBlockD = (String, String) -> (String)->String
    
    var _funcBlockA:funcBlockA = {
        
        () -> Void in
        
        println("***block without any arguments and without return value")
    }
    
    var _funcBlockB:funcBlockB = {
        (stringA:String, stringB:String) ->String in
        
        println("***swift block with arguments and without any return value")
        
        return (stringA + stringB)
    }
    
    var _funcBlockC:funcBlockC = {
        
        (stringA:String, stringB:String) ->(String)->() in
        
        blockC("***wift block with arguments and  return value:\(stringA) and \(stringB)")
        return blockC
    }
    
    var _funcBlockD:funcBlockD = {
        (stringA:String, stringB:String) ->(String)->(String) in
        
        blockD("***swift block as function parameter:\(stringA) and \(stringB)")
        return blockD
    }
    
    
    init(){
        self.testMemberBlock()
    }
    
   private func testMemberBlock(){
        
        ///1. swift block, without any arguments and without return value
        self._funcBlockA()
        //swift block with arguments and without any return value
        var res =  self._funcBlockB("ksnowlv", "kair")
        println("_funcBlockB = result = \(res)")
        
        
        // //swift block with arguments and  return value as function
        var blockC  = self._funcBlockC("ksnowlv", "kair")
        blockC("go shopping")
        
        //swift block as function parameter and  return value as function with parameter and return value
        var blockD  = self._funcBlockD("ksnowlv", "kair")
        var result =  blockD("go shopping")
        println("result blockD = \(result)")
    }
}

2.调用

1
2

var block = QBlock()

3.日志

1
2
3
4
5
6
7
8
***block without any arguments and without return value
***swift block with arguments and without any return value
_funcBlockB = result = ksnowlvkair
 blockC= (***wift block with arguments and  return value:ksnowlv and kair)
 blockC= (go shopping)
blockD = ***swift block as function parameter:ksnowlv and kair
blockD = go shopping
result blockD = blockD = go shopping

三.小结

  • 1.与objective-c中的闭包在使用方法上类似,大体上相同,语法格式上的差异。
  • 2.在内存管理方面:objective-c中需要注意循环引用导致的内存泄漏。而swift依然需要注意该问题。