美文网首页
swift5.0 -- 函数

swift5.0 -- 函数

作者: marlonxlj | 来源:发表于2021-07-13 15:35 被阅读0次
    func test()
    {
        print("222")
    }
    
    test()
    
    func test1() -> Int{
        return 100
    }
    print(test1())
    
    func test2(name: String) -> String{
        return name
    }
    
    print(test2(name: "hello world"))
    
    func test3(name:String...){
    //    print(type(of: name))
        for item in name {
            print(item)
        }
    }
    
    test3(name: "a","b")
    
    func test5(name:(n1:String,n2:Int)) -> (String,Int){
        var value:(a:String, b:Int)
        value.a = name.n1 + " hello"
        value.b = name.n2 + 100
        
        return value
    }
    
    var value = test5(name: (n1:"world",n2:20))
    print(value)
    
    func test1() -> Void
    {
        print("test1()")
    }
    
    var a:() -> Void = test1
    
    a()
    
    //匿名函数
    var b:() -> Void = {() -> Void in
        print("匿名函数")
    }
    b()
    
    print("---------------------")
    
    func test2(param:String, param1:Int) -> String
    {
        return param + String(param1)
    }
    
    var c:(String, Int) -> String = test2
    
    let dc = c("hello", 10)
    print(dc)
    
    var d:(String, Int) -> String = {(param1: String, param2: Int) -> String in
        return param1 + String(param2)
    }
    
    let dd = d("王敏",22)
    print(dd)
    
    print("---------------------")
    
    func test3(param:[Int]) -> String
    {
        var temp: String = ""
        
        for item in param {
            temp = temp + String(item)
        }
        return temp
    }
    
    var e:([Int]) -> String = test3
    
    var value = e([1,2,3])
    print(value)
    
    var f:([Int]) -> String = {(a:[Int]) -> String in
        
        var temp: String = "--"
        
        for item in a {
            
            temp = temp + String(item)
        }
        return temp
    }
    
    //print(f([4,5,6]))
    
    var g:([Int]) -> String = e
    print(g([10,11,12]))
    
    func test()
    {
        print("test()函数,没有参数,没有返回值")
    }
    
    var a:() -> Void = test
    //a()
    
    func test1(param:()->Void)
    {
        param()
    }
    
    //test1(param: test)
    
    test1(param: {() -> Void in
        print("test()函数,没有参数,没有返回值")
    })
    print("-------------------")
    
    func sum(param:(Int,Int) -> Int) -> Int
    {
        var value = param(1,2)
        print("value=\(value)")
        return value
    }
    func add(a:Int, b: Int) -> Int
    {
        return a + b
    }
    //sum(param: add)
    
    var value1 = sum(param: {(a, b) -> Int in
        return a + b
    })
    
    print("323:\(value1)")
    
    var array = [1,3,5,0,2,8]
    
    func paly2(a:Int, b:Int) -> Bool
    {
        if a < b {
            return true
        }else{
            return false
        }
    }
    //array.sort(by: {(a, b) -> Bool in
    //    if a < b {
    //        return true
    //    }else{
    //        return false
    //    }
    //})
    array.sort(by: paly2)
    print(array)
    
    
    func paly1(value:Int) -> Int
    {
        return value * value
    }
    
    func play2(value: Int) -> Int
    {
        return value + value
    }
    
    func test(param:Bool) -> (Int) -> Int
    {
        return param ? paly1 : play2
    }
    
    var a = test(param: true)
    
    print(a(3))
    
    print("-----------------------")
    
    func test1(param:Bool) -> (Int) -> Int
    {
        func play1(value:Int) -> Int
        {
            return value * value
        }
        func paly2(value: Int) -> Int
        {
            return value + value
        }
        
        return param ? play1 : paly2
    }
    var b = test1(param: true)
    
    print(b(4))
    
    var a:() -> Void = {() -> Void in
        print("a")
    }
    
    print(type(of: a))
    
    var b:() -> Void = {
        print("b")
    }
    
    b()
    
    print("====================")
    
    //不带参数,不带返回值
    func test(parm:() -> Void)
    {
        parm()
    }
    test(parm: {print("不带参数不带返回值")})
    
    print("====================")
    
    func test2(param:(Int) -> Void)
    {
        param(10)
    }
    test2(param: {(value: Int) -> Void in
        print(value)
    })
    
    test2(param: {(value) in
        print(value)
    })
    
    print("--------------")
    
    func test3(param: (Int, Int) -> Int)
    {
        print(param(10,20))
    }
    
    //test3(param: {(item1: Int, item2: Int) -> Int in
    //    return item1 + item2
    //})
    
    test3(param: {$0 + $1})
    
    

    相关文章

      网友评论

          本文标题:swift5.0 -- 函数

          本文链接:https://www.haomeiwen.com/subject/rqyrpltx.html