谁能给我解释一下这段 Swift 语法:
func someFunc(label label: String, image: UIImage){}
我想知道第一个 label
参数是什么意思?为什么它没有类型?为什么没有逗号分隔符?
最佳答案
当您为变量指定两个名称时,第一个供函数的调用者使用,第二个在您的函数内部使用。
参见 this documentation ,向下滚动到指定外部参数名称。这是该部分的示例。
这是 sayHello(_:) 函数的一个版本,它接受两个人的名字并为他们两个返回问候语:
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted"))
// prints "Hello Bill and Ted!"
通过为两个参数指定外部参数名称,调用 sayHello(to:and:) 函数的第一个和第二个参数都必须被标记。
使用外部参数名称可以让函数以一种富有表现力的、类似句子的方式被调用,同时仍然提供一个可读且意图清晰的函数体。
关于swift - 谁能解释一下 Swift 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637264/