//: Playground - noun: a place where people can play

import UIKit

///////////////////////////////////////////
//
//func sorted(isOrderedBefore:(T, T)->Bool) -> [T]{
//
//} let animals = ["fish", "cat", "chicken", "dog"] //func isOrderedBefore(one : String, two: String) -> Bool
//{
// return one < two
//}
//
//let sortedString = animals.sort(isOrderedBefore) // 与排序 小于号 ,传递核心代码 let sortedString = animals.sort({(one: String, two: String) -> Bool in
return one < two }) // 在sort 函数里面传递了参数 而参数又是一个函数 ,这个函数就叫做闭包 // 闭包 没有完整的函数声明 有参数列表 one: String, two: String
// in关键字后面是闭包的实现代码 // 编译器可以断言出参数的类型
let sortedString2 = animals.sort({(one, two) -> Bool in
return one < two }) // -> Bool 返回值信息也可以 删除掉 这个信息可以再sort的声明中得到< sort 声明>
let sortedString3 = animals.sort({(one, two) in
return one < two }) // 没有返回值类型->bool声明以后 ()也可以去除掉 let sortedString4 = animals.sort({one, two in
return one < two }) // 可以省略执行代码的return语句 编译器已经断言出来返回值是bool 类型
//所执行代码一行,删除return 语句 let sortedString5 = animals.sort({one, two in
one < two
}) //接下来我们还可以省略参数
// one two 没有意义 用参数本地常量进行代替 let sortedString6 = animals.sort({$0 < $1}) //如果传递的闭包是方法或者函数的最后一个参数, 可以将闭包放到闭包的外面
//称为结尾闭包 let sortedString7 = animals.sort(){$0 < $1}
print(sortedString7) // 还可以移除没有参数的括号
let sortedString8 = animals.sort{$0 < $1}
print(sortedString8) //把花括号替换为小括号 只写一个 < 闭包神奇之处
let sortedString9 = animals.sort(>)
print(sortedString9)
//----------------------------------------------
//闭包还可以捕获 上下文中常量或者变量的数值
//甚至原始环境销毁也可以使用 typealias stateMachineType = () ->Int func makeStateMachine(maxState: Int) -> stateMachineType{ var currentState: Int = 0 return{
currentState++
if currentState > maxState{
currentState = 0
}
return currentState
}
} let tt = makeStateMachine(2) print(tt()) print(tt()) print(tt()) print(tt()) print(tt()) // 不管makeStateMachine 是否在生存期内 都可以捕获makeStateMachine里面的 currentState 变量值 一直存在
//闭包可以超越自身的生命周期捕获外面的变量值

swift 闭包 由浅入深 优化的更多相关文章

  1. Swift闭包概念与常见使用场景总结

    ·Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其他一些 ...

  2. swif(六)swift闭包

    // // main.swift // LessonSwiftSix // // Created by keyan on 15/9/13. // Copyright (c) 2015年 keyan. ...

  3. Swift --闭包表达式与闭包(汇编分析)

    在Swift中,可以通过func定义一个函数,也可以通过闭包表达式定义一个函数! 一.闭包表达式 概念 闭包表达式与定义函数的语法相对比,有区别如下: 去除了func 去除函数名 返回值类型添加了关键 ...

  4. Swift 闭包表达式

    闭包是功能性自包含模块,可以在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C 中的 blocks 以及其他一些编程语言中的 lambdas 比较相似. 闭包的形式主要有三 ...

  5. Swift 闭包(六)

    http://blog.csdn.net/huangchentao/article/details/32714185 闭包 Closures 1.闭包表达式 闭包表达式是一种利用简单语法构建内联包的方 ...

  6. swift 闭包循环引用

    当使用闭包时,类本身持有self,然后又在闭包中访问了self或者self的属性,就会导致恶心额循环引用.swift提供的解决方法是在闭包中定义捕获列表,捕获列表是闭包想怎么引用捕获来的变量.例如下面 ...

  7. swift 闭包

    闭包可以捕获和存储其所在上下文中任意常量和变量的引用. 这就是所谓的闭合并包裹着 这些常量和变量,俗称闭包. Swift标准库中提供了sort排序函数,sort函数的第二个参数是个闭包.和OC中的bl ...

  8. [ios][swift]使用swift闭包进行viewcontroller反向传值

    闭包参考:http://c.biancheng.net/cpp/html/2285.html   闭包详解 传值参考:http://www.tuicool.com/articles/vy2uUz Sw ...

  9. swift 闭包简写实际参数名$0、$1等理解

    Swift 自动对行内闭包提供简写实际参数名,你也可以通过 $0 , $1 , $2 等名字来引用闭包的实际参数值. 如果你在闭包表达式中使用这些简写实际参数名,那么你可以在闭包的实际参数列表中忽略对 ...

随机推荐

  1. 安装mysql-python报错

    运行: pip install mysql-python报错如下: Downloading/unpacking MYSQL-python Downloading MySQL-python-1.2.5. ...

  2. python对比两个文件问题

    写一个比较两个文本文件的程序. 如果不同, 给出第一个不同处的行号和 列号. 比较的时候可以使用zip()函数 a=open('test.txt','r') b=open('test2.txt','r ...

  3. Glide制作圆形图片

    上效果图: 第一步: AndroidStudio添加依赖库: compile 'com.github.bumptech.glide:glide:3.5.2' 第二步: <ImageView an ...

  4. POJPower Network (最大流)

    题目链接. 分析: 这题描述的可不是一般的复杂. 其时就是很多源点.很多汇点,使尽量多流量的到达汇点. 因为有很多源点,就再设一个源点(0号),使得0号到其它源点的容量为其它源点的初始量,同样设一汇点 ...

  5. POJ1573 Robot Motion(模拟)

    题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...

  6. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  7. 【Express】路由

    var express = require('express'); var app = express(); app.set('port', process.env.PORT || 3000); ap ...

  8. [Design Pattern] Adapter Pattern 简单案例

    Adapter Pattern, 即适配器模式,用于连接两个不兼容的接口,属于结构类的设计模式. 或者叫做,转换器模式. 下面是一个转换器模式简单案例. 假设已有 AudioPlayer 专门播放 m ...

  9. win7虚拟机起不来,报错transport vmdb error -44 message the vmware authorization

    运行: services.msc 选择:VMware Authorization Service,运行它

  10. jsp servelet

    servlet是java web应用程序. 1.生命周期:init() .service().destroy()方法. 其中service()包括 doGet() .doPost()方法.默认为get ...