gpt4 book ai didi

ios - Swift 中的可选参数标识符

转载 作者:行者123 更新时间:2023-11-28 10:25:51 29 4
gpt4 key购买 nike

我正在学习 iOS 和 Swift 开发,对函数参数标识符有疑问。这是 UIButton.setText 的签名,当 Xcode 建议提前输入时它会出现:

// Xcode type-ahead signature:
sender.setTitle(title: String?, forState: UIControlState)

当我复制并粘贴它时,它会插入更多信息,如下所示:

// Actual signature from documentation:
sender.setTitle(<#title: String?#>, forState: <#UIControlState#>)

我的用法是这样的:

sender.setTitle("Hello World", forState: UIControlState.Normal)

如您所见,forState 参数标识符是必需的,但title 标识符不是。我如何从 Xcode 的预输入方法签名建议中确定这一点? title: String? 中的 ? 是否表示标识符是可选的?如果不是,那有什么作用?

最佳答案

… the forState argument identifier is required, but the title identifer is not. How can I determine this from Xcode's type-ahead method signature suggestion?

这是模板在屏幕上的样子:

setTitle template

注意第一个占位符包含 title: String? , 而第二个占位符只包含 UIControlState (并且包含 forTitle: )。这就是您如何知道第一个参数不需要关键字,但第二个参数需要。这也会使您的 tab 键自动选择要替换的正确数量的文本。 (如果 tab 不起作用,您也可以使用 Navigate > Jump to Next Placeholder control/。)

您粘贴的副本如下所示:

sender.setTitle(<#title: String?#>, forState: <#UIControlState#>)

<# ... #>是 Xcode 表示占位符的方式。再次注意第一个参数(包括关键字)在 <# ... #> 中。 , 但第二个占位符排除关键字 forState:并且只包含 UIControlState 类型.

您可以使用此语法在您自己的自定义代码段中创建占位符。您只需输入 <##>进入片段。一旦你打完 #> , Xcode 会将带注释的文本转换为占位符。示范:

snippet demonstration

?String 之后指示可选字符串。它是 Optional<String> 的语法糖.您可以了解更多 Optional通过阅读 The Swift Programming Language 中标题为“可选”的部分.

关于ios - Swift 中的可选参数标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915204/

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