swift的基本使用(3)嵌套函数和闭包

/*
//函数嵌套
//内层函数可以使用外层函数的参数

func helloWord (aa:Int) {

func helloLanou () {
    aa
}
println(aa)
helloLanou()

}
helloWord(10)

//hello1要返回一个函数,函数的类型是((String,Int) ->String)
//函数类型
typealias funcType = ((String,Int) ->String)
func hello1 () -> funcType {

func hanshu (str:String, num: Int) ->String{
    return str + "+ \(num)"
}

func hanshu2 (name:String, age: Int) -> String{
    return "info: name:" + name + "age:\(age)"
}
return hanshu2

}
var hanshushu = hello1()
hanshushu(“str”, 99)

typealias funcType2 = ((Int, Int) -> Int)
func hello2 (str:String) -> funcType2 {

func yunsuan (a:Int, b:Int) -> Int {
    switch str {
    case "+":
        return a + b

    case "-":
        return a - b

    case "*":
        return a * b

    case "/" :
        return a / b
    default:
        return 0
    }

}
return yunsuan

}
var temp = hello2(“+”)
var sult = temp(2,3)
sult
/*
func hello3 (str:String) -> ((Int, Int) -> Int)? {

func jia (a:Int, b:Int) ->Int{

    return a + b
}
func jian (a:Int, b:Int) ->Int{
    return a - b
}
func cheng (a:Int, b:Int) ->Int{
    return a * b
}
func chu (a:Int, b:Int) ->Int{
    return a / b
}

switch str {
case "+":
    return jia
case "-":
    return jian
case "*":
    return cheng
case "/":
    return chu
default:
    return nil
}

}

let helloResult = hello3(“hjkh”)
if helloResult == nil {
println(“nil”)
}else{

helloResult!(1, 2)

}
*/

//返回值是函数与函数嵌套不存在依赖关系

func hello3(name:String) ->String {
return “hello (name)”
}

func hello4() -> ((String) -> String){
return hello3
}

*/

//闭包 — 相当于oc 的 block
//{
// (参数) -> 返回值 in
// 函数实现体
//}

//闭包幽冥匿名函数,函数是特殊的闭包

//使用系统排序函数说明闭包的省略机制

var numbers = [11,22,33,77,88,44,55]

var numbersResult = sorted(numbers, { (n1:Int, n2:Int) -> Bool in
return n1 > n2
})
numbersResult

//闭包的参数类型可以省略
var numbersResult1 = sorted(numbers, { (n1, n2) -> Bool in
return n1 < n2
})
numbersResult1

//in 前面的语句都可以省略,没有参数名,所以提供 0 1 的参数获取方式
var numberResult2 = sorted(numbers, {
return 0> 1
})

//极简模式
var numberResult3 = sorted(numbers, >)
numberResult3

//尾随闭包
//如果闭包作为参数列表的最后一个参数,并且实现比较复杂的时候,可放在参数列表的外边,此时称作尾随闭包
var numberResult4 = sorted(numbers){
0> 1
}
numberResult4

func pig1 (name:String) -> String{
return “pig1”
}
//typealias 修饰的类型 对函数和闭包都有效
//类型指得是 参数类型 -> 返回值类型
typealias pig1Type = ((String) -> String)

func pig(canshu:pig1Type) -> pig1Type {
return pig1
}
//闭包常用于参数,函数常用于返回值
pig({
(name:String) -> String in
return “pig1”
})

//”3” + “4” = “7”
//”3” + “4” = 7
typealias myType = ((String, String) -> Int)

func closure (clo:myType) -> String {
let result = clo(“3”, “4”)//得到7
return result.description//转成字符串
}

let str:String = closure({
(a:String, b:String) -> Int in
return a.toInt()! + b.toInt()! //转成Int

})
str

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值