ios深度解析之Swift(函数)

来源:互联网 发布:淘宝商家信用卡费率 编辑:程序博客网 时间:2024/05/29 13:24
// 函数// 语法格式//func name (参数列表) -> 返回值 {//    // 函数实现体//}// .无参, 无返回值func hello1() {    print("hello1");}func hello2() -> Void {    print("hello2")}// 函数调用hello1()


// .有参// 1,关于内部参数名// 只在函数作用域内部使用的参数名func hello3 (name: String , name2: String) {    print("你好\(name) and \(name2)")}// 除了第一个参数 后面的参数在调用的时候会自动生成一个与外部参数名相同的外部参数名// 原则上 第一个参数的外部参数名应该写在函数名的末尾hello3("小林林", name2: "猪八戒")func hello4 (mingzi1 name: String, mingzi2 name2: String) {    print("你好\(name) and \(name2)")}// 内部参数名的添加规则 在外部参数名前面声明中间用空格隔开hello4(mingzi1: "小林林", mingzi2: "猪八戒")// 使用占位符 _ 省略掉自动生成的外部参数名func hello5 (name: String,_ name2: String) {    print("你好 \(name) and \(name2)")}hello5("小林林",  "猪八戒")// 可以为参数赋初始值 调用时传参则更改 不传参使用初始值func hello6 (name: String = "张三", name2: String = "李四") {    print("你好 \(name) and \(name2)")}//hello6()hello6(name2:"王五")

// 有返回值情况// 返回值直接给类型func hi1() ->String {    return "hehehheheh"}hi1()// 输出元音和辅音个数func count (tempStr: String) -> (Int , Int) {    var number1:Int = 0    var number2:Int = 0        for num in tempStr.characters {        switch num {            case "a", "e", "i", "o", "u":            number1++                    default:            number2++                    }    }    return (number1, number2)}count("gerageargshshsshsthjstjhshshnaiou")

// 不确定参数个数. 参数个数可变func count2 (numbers:Int...) ->Int {    var sum = 0    // 不确定个数的参数, 作为数组使用    for num in numbers {        sum += num    }    return sum}count2(1,2,3,4,5,6)// 参数在函数体内 默认是不可变的,用var修饰之后才可变func count3 (var a:Int, b:Int) -> Int {//    a += b    a++//   b++    return a}var c = 10count3(c , b: 0)// inout 修饰参数 将外部变量的地址传进来 从而改变外部变量的值func changeValue (inout a:Int) {    a++}changeValue(&c)c

//声明一个函数 实现功能 传入 "+", "-", "/".的字符串 返回对应运算的函数 : "+" --- 返回 Int + Int = Int// 函数的返回值是函数 可以用函数嵌套的形式实现 但并不是必须使用函数嵌套// 方法一func hello3(i:String)-> ((Int, Int) -> Int) {        func sum(a:Int, b:Int) -> Int {                switch i {            case "+":            return a + b            case "-":            return a - b            case "/":            return a / b            case "*":            return a * b        default:            return 0        }            }     return sum        }let hanshu1 = hello3("+")hanshu1(1, 2)let hanshu2 = hello3("-")hanshu2(1, 2)let hanshu3 = hello3("*")hanshu3(1, 2)let hanshu4 = hello3("/")hanshu4(4, 2)let hanshu5 = hello3("+")let result = hanshu5(1, 2)// 方法二    func sum1(a:Int, b:Int) -> Int {        return a - b    }    func sum2(a:Int, b:Int) -> Int {        return a + b    }    func sum3(a:Int, b:Int) -> Int {        return a * b    }    func sum4(a:Int, b:Int) -> Int {        return a / b    }    func hello3(i:String)-> ((Int, Int) -> Int)? {        switch i {        case "+":            return sum2        case "-":            return sum1        case "/":            return sum3        case "*":            return sum4        default:            return nil        }        }let han = hello3("+")let result = han!(1, 2)

// 指定类型别名typealias funcType = ((Int, Int) ->Int)typealias funcType2 = Stringfunc func3 () ->funcType {    func hanshu3(num1: Int, num2: Int) -> Int {        return 0    }    return hanshu3}


// ++++++++++++++++++++++++ 闭包: 相当于block// 闭包 又叫做 匿名函数//{////    (参数列表) -> 返回值 in 函数的实现体//}// 使用系统排序函数对闭包的省略机制进行说明var numbers = [11, 44, 33, 66, 22]var numbers_result = numbers.sort({    (n1:Int, n2:Int)-> Bool in    return n1 > n2})// 闭包的参数类型可以省略// 根据上下文进行类型推断var numbers_result1 = numbers.sort({    (n1, n2)-> Bool in    return n1 > n2})// in 前面的语句 可以省略// 此时 没有参数名 自动生成$0, $1 进行替代var numbers_result2 = numbers.sort({        return $0 > $1 // 此处 return也可以省略})// return 也可以省略 条件是:函数实现体内只有return语句var numbers_result3 = numbers.sort({         $0 > $1 // 此处 return也可以省略})// 极简模式var numbers_result4 = numbers.sort(>)// +++++++++++以上是闭包的省略机制// 尾随闭包// 当闭包作为参数列表的最后一个参数时 并且内容相对复杂 可以尾随在参数列表的外面var numbers_result5 = numbers.sort(){        (n1, n2)-> Bool in    return n1 > n2    }typealias myType = ((String) -> String)func getPigName(name:String)-> String {    return "pig = \(name)"}func pig(canshu:myType)->myType {        return getPigName}pig({    (name:String)-> String in        return "pig = \(name)"})// 闭包常用于参数 函数常用于返回值// "3" + "4" = "7"// "3" + "4" = 7typealias myType2 = ((String, String) -> Int)func closure (clo:myType2) ->String {    let result_clo = clo("3" , "4")        return result_clo.description // 转成字符串    }closure({    (a:String, b:String)-> Int in    return Int(a)! + Int(b)!    })


1 0
原创粉丝点击