Swift 4自定义参数标签-是否必需? [英] Swift 4 custom argument labels - required?

查看:106
本文介绍了Swift 4自定义参数标签-是否必需?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

func greet (person person: String, on day: String) -> String {
    return "Hello \(person) on \(day)"
}

可以执行为

打招呼(约翰","23")

greet ("John", "23")

快捷键4-自定义参数标签的行为与无参数标签的行为相同-"_" .

Swift 4 - custom argument labels behave as no argument label - "_".

如何使自定义标签强制执行?

How to make custom labels mandatory for execution?

打招呼(人:约翰",上:"23")

greet (person: "John", on: "23")

谢谢.

编辑-问题是不正确的,因为必须使用自定义标签

Edit - question is incorrect, as custom labels are mandatory

推荐答案

Swift 中,每个函数参数可以同时具有 自变量标签 和一个 参数名称 .

In Swift each function parameter can have both argument label and a parameter name.

  1. 参数标签 :在函数调用中使用
  2. 参数名称 :在函数定义中使用

默认情况下,参数使用其参数名称作为其参数标签.

By default, parameters use their parameter name as their argument label.

以下是可以在swift中使用的可能的函数声明:

These are the possible function declarations that can be used in swift:

案例1: 参数名称参数标签

func greet (person: String, day: String) -> String {
    return "Hello \(person) on \(day)"
}

函数调用: greet(人:"John",日期:"23")

案例2:不同的参数名称参数标签

func greet (person person: String, on day: String) -> String {
    return "Hello \(person) on \(day)"
}

函数调用: greet(人:"John",上:"23")

案例3:,使用 _ 作为参数标签

func greet (_ person: String, _ day: String) -> String {
    return "Hello \(person) on \(day)"
}

函数调用: greet("John","23")

有关函数参数如何工作的更多信息,可以参考:

For more on how function parameters work, you can refer to: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

让我知道您是否仍然遇到任何问题.

Let me know if you still face any issues.

这篇关于Swift 4自定义参数标签-是否必需?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆