swift - 谁能解释一下 Swift 语法?

标签 swift syntax swift2

谁能给我解释一下这段 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/

相关文章:

postgresql - 在 PostgreSQL 中将 $$ 放入美元引用的字符串中

c++ - 为什么没有参数标识符的函数在 C++ 中有效?

json - 如何解决错误 : "Invalid type in JSON write (NSConcreteData)"

ios - 将文本字段限制为一位小数点输入,仅限数字,以及小数点后两个字符 - Swift 3

C++ 加密库在 Apple Swift 中的使用

arrays - 如何创建一个数组或字典,其值只能是 String、Int 和 Boolean?

php - 如何使用另一个对象实例初始化 PHP 类的实例?

swift - 在 swift 2.2 中无法正确推断可选类型

swift - 使用 readLine() 跳过行 - Swift 2.2

iOS Facebook 登录崩溃