【问题标题】:can i use assignement operator in methods parameters我可以在方法参数中使用赋值运算符吗
【发布时间】:2018-10-01 04:53:37
【问题描述】:
open func open(_ url: URL, options: [String : Any] = [:], completionHandler completion: ((Bool) -> Swift.Void)? = nil)

因为我注意到 UIapplication 中的开放函数在其参数中使用赋值运算符是可能的,有人请详细说明上述代码行中发生的事情并为我解释每个元素

【问题讨论】:

标签: ios swift


【解决方案1】:

您可以通过在参数类型之后为参数分配值来为函数中的任何参数定义默认值。如果定义了默认值,则在调用函数时可以省略该参数。

 func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
// If you omit the second argument when calling this function, then
// the value of parameterWithDefault is 12 inside the function body.
}


someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) //parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12

【讨论】:

  • 如果没有给出默认值,我必须要传递值吗?
  • @velsserver 是的
猜你喜欢
  • 1970-01-01
  • 2021-10-22
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多