美文网首页
Swift 函数默认值的猜测

Swift 函数默认值的猜测

作者: craig_wang | 来源:发表于2017-10-25 17:54 被阅读58次

一. 在swift函数的申明语法中,可以给参数设置默认值,代码如下:

func test(name: String = "") {
    print(#function)
}
test() /// 调用一
test(name: "tom") /// 调用二
  • 有默认值的函数,可以省略参数的传递 调用一
  • 按正常函数调用 调用二

二. 这种函数是怎么实现的呢?

猜测:编译器会对应生成多个函数:

func test(name: String) {
    /// -> 相当于1种写法
    /// test(name: String)
    print(#function)
}
func test(name: String, address: String = "") {
    /// -> 相当于2种写法
    /// test(name: String)
    /// test(name: String, address: String)
    print(#function)
}

从上面的函数来看 test(name: String) 就被重复申明了,那么应该报错啊!不不不,我们只能说它可能报错,请看下面的代码,代码比较长,请耐心阅读

  • 正确情况:
/// 同名不同参
func test(name: String) {
    /// -> 相当于1种写法
    /// test(name: String) -> 和自身行式一样的,假设为特殊,可以覆盖其他(不能被其它覆盖)specialFunc
    print(#function)
}
func test(name: String, address: String = "") {
    /// -> 相当于2种写法
    /// test(name: String)
    /// test(name: String, address: String) specialFunc
    print(#function)
}
func test(name: String, age: Int = 18) {
    /// -> 相当于2种写法
    /// test(name: String)
    /// test(name: String, age: Int) specialFunc
    print(#function)
}
func test(name: String, age: Int = 18, address: String = "") {
    /// -> 相当于4种写法
    /// test(name: String)
    /// test(name: String, age: Int)
    /// test(name: String, address: String)
    /// test(name: String, age: Int, address: String) specialFunc
    print(#function)
}

/// 由于specialFunc的覆盖和唯一性
/// 上面的函数最终得到如下4个(仅有)唯一函数
/// test(name: String) specialFunc
/// test(name: String, address: String) specialFunc
/// test(name: String, age: Int) specialFunc
/// test(name: String, age: Int, address: String) specialFunc

/// 这里的调用一一对应specialFunc函数, 没有歧义,运行正常
test(name: "tom") /// test(name:)
test(name: "tom", address: "") /// test(name:address:)
test(name: "tom", age: 10) /// test(name:age:)
test(name: "tom", age: 10, address: "") /// test(name:age:address:)
  • 会产生错误的情况如下:
func parameter(name: String, address: String = "", age: Int = 18) {
    /// -> 相当于4种写法
    /// parameter(name: String)
    /// parameter(name: String, address: String)
    /// parameter(name: String, age: Int)
    /// parameter(name: String, address: String, age: Int) specialFunc
    print(#function)
}
func parameter(name: String, age: Int = 18, address: String = "") {
    /// -> 相当于4种写法
    /// parameter(name: String)
    /// parameter(name: String, age: Int)
    /// parameter(name: String, address: String)
    /// parameter(name: String, age: Int, address: String) specialFunc
    print(#function)
}

/// 上面的到6个有歧义的函数:
/// parameter(name: String)
/// parameter(name: String, address: String)
/// parameter(name: String, age: Int)
/// 和
/// parameter(name: String)
/// parameter(name: String, age: Int)
/// parameter(name: String, address: String)

/// 无法确认函数的唯一性,无法正确的调用函数,报错!
/// parameter(name: "")
/// parameter(name: "", age: 10)
/// parameter(name: "", address: "")

/// 上面的函数得到两个唯一函数:
/// parameter(name: String, address: String, age: Int) specialFunc
/// parameter(name: String, age: Int, address: String) specialFunc
/// 调用唯一函数,正确
parameter(name: "", address: "", age: 18)
parameter(name: "", age: 18, address: "")
  • 如上的代码可以直接运行来校验猜测
  • 就specialFunc而言,确实是出于对结果的构想(假定就是这样来推断后面的结果)

三. 以上是我个人的猜测。如有更好的解释,请各位多多指教。

相关文章

  • Swift 函数默认值的猜测

    一. 在swift函数的申明语法中,可以给参数设置默认值,代码如下: 有默认值的函数,可以省略参数的传递 调用一 ...

  • Swift基础-04(函数 闭包)

    1.Swift中函数的使用 函数的定义 外部参数 _的使用 常见的 "_" 在for循环中 函数的默认值 无返回值...

  • swift002

    有来写学习swift的笔记了。。。。 函数的默认值 没有返回值的函数 使用常量记录函数 闭包 在OC中block是...

  • Swift5.0 监听键盘写法

    其他: 忽略具有默认值的参数的外部参数名 当函数(或者方法)的参数具有默认值时,Swift自动为该参数提供与参数名...

  • 1.0 C++远征:函数参数默认值、函数重载、内联函数

    C++远征 [TOC] 一、函数参数默认值 1.函数参数默认值的声明 2.函数参数默认值的定义 3.函数参数默认值...

  • Learning iOS D3 2017-10-24

    函数学习 学习swift函数 设置默认值 函数是一种特殊的闭包 有三种无返回值的传递方式 闭包 主要用于: 1.异...

  • 4.函数的扩展

    函数参数的默认值 解构赋值默认值的使用: 函数的length属性:指定了默认值以后,函数的length属性,将返回...

  • ES6 函数的扩展

    函数参数的默认值 ES6允许为函数的参数设置默认值,即直接写在参数定义的后面 函数参数默认值与解构赋值默认值结合使...

  • Swift 函数指定参数默认值

    swift 中参数一半定义如下: 例子 带默认值 喜欢可以加Q群号:874826112,一起学习,成长,交流工作经...

  • C语言和C+的这些区别你知道吗?

    1.带有默认值的函数 在C语言里面没有带默认值的函数,C++支持带默认值的函数。 在给形参默认值的时候,要按照从右...

网友评论

      本文标题:Swift 函数默认值的猜测

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