Swift函数,一个参数的两个名称[复制]

时间:2020-12-18 15:02:55

This question already has an answer here:

这个问题已经有了答案:

I have noticed that some methods e.g. init(nibName nibName: String?, bundle nibBundle: NSBundle?) has two "names" for one parameter except the first one is not possible to use inside method. In this case you are not able to use bundle but can use nibBundle. For example, when I call super.init(nibName: nibName, bundle: bundle) I get error "Use of unresolved identifier 'bundle'".

我注意到一些方法,例如init(nibName nibName: String?, bundle nibBundle: NSBundle?)对于一个参数有两个“名称”,但第一个参数在inside方法中不可能使用。在这种情况下,您不能使用bundle,但可以使用nibBundle。例如,当我调用super。init(nibName: nibName, bundle: bundle)我得到错误“使用未解析的标识符包”。

My question is: What is it (double named parameter) for? How to use it properly?

我的问题是:它(双命名参数)是做什么用的?如何正确使用它?

EDIT: It is now obvious it is External Parameter Names thing. I have subclass of UIViewController and override following method. I don't get where nibBundle gets from? Apparently it is not defined in function header.

编辑:现在很明显,它是外部参数名称的东西。我有UIViewController的子类并覆盖下面的方法。我不知道nibBundle从哪里来?显然,它在函数头中没有定义。

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
   super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
   let someBundle = nibBundle
   print(someBundle)
}

2 个解决方案

#1


1  

From Apple's documentation:

从苹果的文档:

Sometimes it’s useful to name each parameter when you call a function, to indicate the purpose of each argument you pass to the function.

有时,在调用函数时为每个参数命名,以表明传递给函数的每个参数的目的是有用的。

If you want users of your function to provide parameter names when they call your function, define an external parameter name for each parameter, in addition to the local parameter name. You write an external parameter name before the local parameter name it supports, separated by a space:

如果您希望函数的用户在调用函数时提供参数名称,则为每个参数定义一个外部参数名称,并添加本地参数名。在它所支持的本地参数名之前,您要写一个外部参数名,用空格隔开:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

Shorthand External Parameter Names

速记外部参数名称

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.

如果您想为函数参数提供一个外部参数名称,并且本地参数名称已经是一个合适的名称,那么您不需要为该参数编写相同的名称两次。相反,只需编写一次名称,并在名称前面加上一个散列符号(#)。这告诉Swift将该名称用作本地参数名和外部参数名。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

#2


0  

First name is public name, second - private (ca be used only in function)

第一个名称是公共名称,第二个是私有的(仅用于函数中)

#1


1  

From Apple's documentation:

从苹果的文档:

Sometimes it’s useful to name each parameter when you call a function, to indicate the purpose of each argument you pass to the function.

有时,在调用函数时为每个参数命名,以表明传递给函数的每个参数的目的是有用的。

If you want users of your function to provide parameter names when they call your function, define an external parameter name for each parameter, in addition to the local parameter name. You write an external parameter name before the local parameter name it supports, separated by a space:

如果您希望函数的用户在调用函数时提供参数名称,则为每个参数定义一个外部参数名称,并添加本地参数名。在它所支持的本地参数名之前,您要写一个外部参数名,用空格隔开:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

Shorthand External Parameter Names

速记外部参数名称

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.

如果您想为函数参数提供一个外部参数名称,并且本地参数名称已经是一个合适的名称,那么您不需要为该参数编写相同的名称两次。相反,只需编写一次名称,并在名称前面加上一个散列符号(#)。这告诉Swift将该名称用作本地参数名和外部参数名。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

#2


0  

First name is public name, second - private (ca be used only in function)

第一个名称是公共名称,第二个是私有的(仅用于函数中)