Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios swift方法参数_Ios_Swift - Fatal编程技术网

ios swift方法参数

ios swift方法参数,ios,swift,Ios,Swift,我正在阅读《快速入门苹果指南》。它们给出了以下代码: required init?(coder aDecoder: NSCoder) 为什么它们是两个参数名称?编码员和编码员 我不明白为什么我们不使用这种语法来: required init?(coder: NSCoder) 谢谢。编码器是外部参数名称。调用方法时使用: SomeClass(coder: someCoder) aDecoder是本地参数名。它用于内部方法: required init?(coder aDecoder: N

我正在阅读《快速入门苹果指南》。它们给出了以下代码:

required init?(coder aDecoder: NSCoder) 
为什么它们是两个参数名称?编码员和编码员

我不明白为什么我们不使用这种语法来:

required init?(coder: NSCoder) 

谢谢。

编码器是外部参数名称。调用方法时使用:

SomeClass(coder: someCoder)
aDecoder
是本地参数名。它用于内部方法:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
您可以只指定本地参数名称:

required init?(coder: NSCoder)
在这种情况下,外部参数名称自动采用相同的值
coder
。 主要用途是可读性。你的例子不太明显。让我们从Eric D发表的文章中选一个更好的:

func sayHello(to person: String, and anotherPerson: String) -> String {
    return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted")) 
当调用
sayHello
方法时,这比

func sayHello(person: String, anotherPerson: String) -> String {
    return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(person: "Bill", anotherPerson: "Ted"))
你当然会写作

func sayHello(to: String, and: String) -> String {
    return "Hello \(to) and \(and)!"
}
print(sayHello(to: "Bill", and: "Ted"))

但在这种情况下,局部变量使用了错误的命名样式

外部/内部参数名称:在实践中,使用以下下划线技巧来避免愚蠢的“命名”参数样式——这是Swift中唯一的灾难性想法
private func\u hide(a:Bool、\b:Bool、\c:Bool、\ud:Bool)
请注意,所有函数都使用下划线,但第一个函数不使用下划线。