swift语言中'#'标记是什么意思 [英] What is the meaning of the '#' mark in swift language

查看:43
本文介绍了swift语言中'#'标记是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这样的代码:

func hello(name: String, #helloMessage: String) -> String { 
    return "\(helloMessage), \(name)." 
} 

我的问题是# 标记在参数名称之前是什么意思?这是否意味着调用函数时必须指定参数?

My question is what # mark means before parameter's name? Is that meaning that the parameter has to be specified when calling a function?

此外,谁能告诉我没有这个 # 标记的功能的不同之处?非常欢迎代码示例.

Moreover can anyone show me a difference with the function without this # mark? Code examples are more than welcome.

推荐答案

更新 (Swift 3.*...)

第一个参数签名的默认行为发生了巨大变化.要了解参数标签(例如外部参数")和参数名称(例如本地参数")的工作原理,请阅读 Apple Swift 手册中的函数参数标签和参数名称"一章.

the default behavior of the first parameter’s signature was changed drastically. To understand how argument labels (ex. "external parameters") and parameter names (ex. "local parameters") work, please read the chapter "Function Argument Labels and Parameter Names" from the Apple’s Swift-book.

一些例子:

func someFunction(parameterName: Int) { parameterName }
someFunction(parameterName: 5) // argument label not specified

func someFunction(argumentLabel parameterName: Int) { parameterName }
someFunction(argumentLabel: 5) // argument label specified

func someFunction(_ parameterName: Int) { parameterName }
someFunction(5) // argument label omitted

方法和函数之间的这种行为没有区别.

There is no difference in this behavior between methods and functions.

更新 (Swift 2.*)

不推荐使用下面描述的功能,需要将参数名称写入两次才能获得与之前使用哈希符号相同的行为.

The feature described below was deprecated, one need to write the parameter name twice to get the same behavior as with hash symbol before.

更新(示例)

对于函数:当函数被调用并且某些参数的用途不明确时,您可以为这些参数提供外部名称.

For functions: when the function is called and purpose of some parameters is unclear, you provide external names for those parameters.

func someFunction(parameterName: Int) { parameterName }
someFunction(5) // What is the meaning of "5"? 

func someFunction(externalParameterName parameterName: Int) { parameterName }
someFunction(externalParameterName: 5) // Now it's clear.

但如果外部名称和本地名称相同,则只需在参数名称前写一个哈希符号即可.

But if external and local names are the same, you just write a hash symbol before the parameter name.

func someFunction(#parameterName: Int) { parameterName }
// It's actually like:
// func someFunction(parameterName parameterName: Int) { parameterName }
someFunction(parameterName: 5)

对于方法:默认情况下,第一个参数名称仅是本地的(如函数),但第二个和后续参数名称既是本地的又是外部的(就像您在参数名称前写一个哈希符号一样,这个# 是隐式存在的):

For methods: by default first parameter name is only local (like by functions), but second and subsequent parameter names are both local and external (like as you write a hash symbol before the parameter name, this # is implicitly there):

class SomeClass {
    func someMethodWith(firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(5, andSecondParameter: 10)

您也可以对方法的第一个参数使用 #(或添加一个明确的外部名称),但它不会匹配 Objective-C 风格的调用.

You can use # (or add an explicit external name) for the first parameter of the method too, but it'll not match Objective-C-style calling.

class SomeClass {
    func someMethodWith(#firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(firstParameter: 5, andSecondParameter: 10)

<小时>

原答案

如果你想为一个函数提供一个外部参数名参数,并且本地参数名称已经是合适的名称要使用,您不需要为此写两次相同的名称范围.相反,将名称写一次,并在名称前加上哈希符号 (#).这告诉 Swift 使用该名称作为本地参数名称和外部参数名称.

If you want to provide an external parameter name for a function parameter, and the local parameter name is already an appropriate name to use, you do not need to write the same name twice for that parameter. Instead, write the name once, and prefix the name with a hash symbol (#). This tells Swift to use that name as both the local parameter name and the external parameter name.

摘自:Apple Inc.The Swift Programming Language".电子书.https://itunes.apple.com/ru/book/swift-programming-language/id881256329?l=en&mt=11

Excerpt From: Apple Inc. "The Swift Programming Language." iBooks. https://itunes.apple.com/ru/book/swift-programming-language/id881256329?l=en&mt=11

这篇关于swift语言中'#'标记是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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