swift 函数定义语法

swift function definition syntax

我试图理解 'didFinishPickingMediaWithInfo' 在以下 swift 函数定义中的含义。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}

我看到'info'表示字典集合类型,但我不明白为什么它前面有'didFinishPickingMediaWithInfo'。 我是 swift 和 iOS 开发的新手,请帮助回答可能是微不足道的问题。 谢谢

来自documentation

Modifying External Parameter Name Behavior for Methods

Sometimes it’s useful to provide an external parameter name for a method’s first parameter, even though this is not the default behavior. To do so, you can add an explicit external name yourself.

Conversely, if you do not want to provide an external name for the second or subsequent parameter of a method, override the default behavior by using an underscore character (_) as an explicit external parameter name for that parameter.

这意味着在你的方法中,单词didFinishPickingMediaWithInfo是调用方法时使用的变量名。但是,您从 info 变量中获取数据。例如,在您的情况下,该方法将被称为 imagePickerController(picker: <UIImagePickerController>, didFinishPickingMediaWithInfo: <info>),因为 didFinishPickingMediaWithInfo 替代了 info

希望对您有所帮助!

这允许您为更冗长的版本提供某种别名的参数。因此,无需使用 didFinishPickingMediaWithInfo 作为变量进行操作,您只需使用名为 info.

的局部参数(或别名)

Specifying External Parameter Names

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
}

来源:https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID167