gpt4 book ai didi

Swift 所需参数逻辑

转载 作者:行者123 更新时间:2023-11-30 10:15:40 25 4
gpt4 key购买 nike

在 Swift 中,您有多种可能的方法来声明方法。您可以使用 # 和 _ 符号强制使用参数名称,还可以更改参数的外部定义。

编译器以某种方式为以下方法创建错误消息:

func createCoffee(withSugar sugar:Bool, andMilk milk:Bool){
}

// Where I want to call it like this:
createCoffee(withSugar:true, andMilk: true)

强制我声明方法如下:

func createCoffee(# sugar:Bool, andMilk milk:Bool){
}

// Resulting in the following way to call it
createCoffee(sugar:true, andMilk: true)

这是一件奇怪的事情,声明如下的方法不会导致这个强制的com

func createCoffee(firstParam sugar:Bool, andMilk milk:Bool){
}

// Resulting in the following way to call it
createCoffee(firstParam:true, andMilk: true)

编译器以什么方式决定允许不同的外部参数名称?

最佳答案

外部参数名称用于更详细的描述。将外部参数名称设置为“with_____”会使 Swift 和 XCode 认为它是多余的。从技术上讲,第一个参数始终是“with___ 内部参数名称”。这纯粹是为了方便语法阅读。

关于Swift 所需参数逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com