美文网首页
Swift3.0- 函数和闭包

Swift3.0- 函数和闭包

作者: 元昊 | 来源:发表于2017-08-17 15:53 被阅读10次

函数的几种类型

1.无参无返

func great() -> Void {
            
        }
        func great() {
            
        }

2.有参无返

 func includeParameterNoneReturnValue(person: String, day : String) {
            
            print("Hello \(person), today is \(day)")
        }
        
        includeParameterNoneReturnValue(person: "Judy",day: "23")

2.1 省略外部参数名

        
        func omitOutParameter(_ person : String, _ day : String) -> String {
            
            return "Hello \(person), today is \(day)."
            
        }
        
        print(omitOutParameter("流川枫", "8"))

3.有参有返

        func includeParameterAndReturnValue (_ person : String, _ day : String) -> String {
            return "Houston player \(person) join at \(day)"
        }
        
        print(includeParameterAndReturnValue("周琦", "9"))

4.无参有返

func NoneParameterReturnValue () -> String {
            return "I hate bitch!"
        }

参数和返回值

1.参数可以是那些?

基本类型的值,对象,数组,字典,元组,可变数量的参数,函数,闭包函数,协议,结构体,枚举值

2.怎么定义参数

//a.单值
        
        func calculate(a:Int) {
            let b = a
        }
        
        //b. 多值
        func calculates(a: Int...) {
            for _ in a {
                
            }
        }
        
        //c. 元祖
        func calculatess(a:(name:String, age:Int)){
            let name = a.name
            let age = a.age
        }
        
        //d. 数组
        func calculate_s(a:[String]){
            
            for student in a {
                
            }
        }
        
        //e. 字典
        
        func calculate_d (a:[String:Int]){
            
            for student in a {
                print(student.key)
                print(student.value)
            }
            
        }
        
        //f. 函数作为参数
        
        func add(a:Int,b:Int) -> Int {//作为函数参数的函数
            return a + b
        }
        
        func calculate_fa(asd:(Int,Int) -> Int) {//定义的参数为函数的函数
           print( asd(3,5))
        }
        
        calculate_fa(asd: add(a:b:))
        
        //g. 闭包写法
        
        calculate_fa { (a, b) -> Int in
            
            return a + b
        }
        
        calculate_fa{ (a, b) in a + b }//省略写法(由于swift有推断能力,这样写就能够帮你推断出上面的写法)

        //h. 参数为协议的方法
//        protocol Player{  // 定义协议
//            func play()
//        }
//        
//        func playMusicWithPlayer(player:Player){
//            player.play()
//        }
        
        //i. 参数为结构体
        
//        struct Student{
//            var name:String
//            var age:Int
//        };
//        
//        func getStudentDescription(student:Student){
//            print(student.name)
//            print(student.age)
//        }
        
        //j.  参数为枚举类型
        
        // 定义枚举值
        enum CarType:String{
            case Lincoln = "林肯"
            case MERCURY = "水星"
            case SUZUKI = "铃木"
        }
        // 参数为协议的方法
        func describeCar(carType:CarType){
            print(carType.rawValue);
        }

函数内部定义函数

        func getValueByFlag(flag:Bool) -> (Int, Int) -> Int {
            
            func add_f(a:Int, b:Int) -> Int {
                return a + b
            }
            
            func descrase(a:Int,b:Int) -> Int {
                return a - b
            }
            
            if flag {
                return add
            } else {
                return descrase
            }
        }
        
        let addFunc = getValueByFlag(flag: false)
        
        print(addFunc(2,4))

设置默认参数值

        func addStudent(student:(name:String, nunbers:Double)=("姓名",23554411)){
            print(student.name)
            print(student.nunbers)
        }
        
        addStudent()
        addStudent(student: ("元昊",111307302))

inout的使用

需求: 创建一个函数,交换两个Int类型值

a.如果参数为let修饰的常量

func swapTwoInts( a:  Int, b: Int){
let temporaryA = a
a = b
b = temporaryA
}

报错:系统提示错误,说常量不能修改值

b.我们将参数变成变量var

func swapTwoInts( var a:  Int, var b: Int){
let temporaryA = a
a = b
b = temporaryA
}

报错,不能使用var 修饰参数

c.inout 修饰的参数可以修改值

func swapTwoInts(  a: inout Int, b:inout Int){
    let temporaryA = a
    a = b
    b = temporaryA
}
var a = 30
var b = 40
swapTwoInts(a: &a, b: &b)
print(a)
print(b)


40
30

Warning:

1.inout的位置 在: 后面,数据类型前面
2.inout 修饰的参数不能有默认值
3.inout 不能用于修饰多值(如a:Int...)

others:函数类型的变量不能用标签修饰参数

// 错误的写法  不能使用a,b标签
var swap1:( a :inout Int, b: inout Int)->Void = swapTwoInts
// 你应该像下面这样
var swap1:( _ :inout Int, _: inout Int)->Void = swapTwoInts
// 或者下面这样也可以,a,b 不一定要和实际函数对应
var swap1:( _ a:inout Int, _ b: inout Int)->Void = swapTwoInts
// 建议还是用下面这种
var swap1:( inout Int, inout Int)->Void = swapTwoInts

定义闭包类型数据

        var customersInLine = ["Chirs","Alex","Ewa"]
        let customerprovider = { customersInLine.remove(at: 0)}
        print(customersInLine.count)
        print("Now Serving \(customerprovider())!")
        print(customersInLine.count)

提示:上面那种闭包其实是无参有返的闭包形式,原形如下

let customerProvider:()->String= { customersInLine.remove(at: 0)}

相关文章

  • Swift3.0- 函数和闭包

    函数的几种类型 1.无参无返 2.有参无返 2.1 省略外部参数名 3.有参有返 4.无参有返 参数和返回值 1....

  • Swift3.0-函数和闭包

    使用func 声明一个函数。通过函数名称和参数调用一个函数。使用->区分参数名和函数返回的类型。 函数的参数可以有...

  • rust 闭包与同步

    rust 闭包与同步 rust 闭包 rust闭包中主要包括两个部分,闭包参数和闭包环境变量。闭包函数和函数参数使...

  • 函数对象和闭包

    函数对象和闭包 一) 函数对象 示例: 二)函数嵌套 三)闭包函数

  • 面试题(day-2)

    1 ,什么是闭包?闭包有什么好处?使用闭包要注意什么? 闭包:函数嵌套函数,内部函数可以引用外部函数的参数和变量,...

  • Swift-闭包

    闭包的特点:一个函数有权访问另外一个函数内的变量和参数 闭包—匿名函数 结尾闭包 衍生 内容捕获 闭包和函数是引用类型

  • 闭包函数

    闭包函数 闭包,又称闭包函数或者闭合函数,其实和嵌套函数类似,不同之处在于,闭包中外部函数返回的不是一个具体的值,...

  • php之闭包函数(Closure)

    php闭包函数(Closure) JS闭包 js和php闭包使用和区别

  • 变量

    闭包: 什么是闭包? 函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾机构回收; 用闭包做...

  • 变量和函数、闭包

    闭包: 什么是闭包? 函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾机构回收; 用闭包存...

网友评论

      本文标题:Swift3.0- 函数和闭包

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