Swift 中的方法名称

Method names in Swift

在 Objective-C 中,我们有类似 application:didFinishLaunchingWithOptions: 的方法名称,但在 Swift 中,相同作业的方法看起来不同。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

这个方法的名称是否简单application,因为其他一切都只是参数?还是名称中带有 space 的 application didFinishLaunchingWithOptions? 我在 Apple 文档中寻找官方答案,但找不到。

文档中的名称是什么意思?它是 application(_:didFinishLaunchingWithOptions:) 我无法想象需要手动调用它的情况,但以防万一:

    application(app, didFinishLaunchingWithOptions:aDictionary) 

方法确实“恰到好处”application.
Swift 更经常使用它,如果你有一个 tableview 例如几乎所有函数都以 tableview

开头
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}

参数“定义”函数而不是方法。

两种语言的命名完全相同。

如果您想要一个好的证明,请尝试在选择器中使用任何其他命名。

该方法确实被称为application,但是didFinishLaunchingWithOptions是一个外部参数名称并且:

If you provide an external parameter name for a parameter, that external name must always be used when you call the function.

由于可以有两个名为 application 的函数具有不同的外部参数名称,因此我们在引用函数时总是必须指定外部参数。因此,function/method 的全名将是

application(_:didFinishLaunchingWithOptions:)

你是对的,目前还没有任何约定来引用 Swift 函数。现在引用函数最安全的方法是使用 Obj-C 约定。

application:didFinishLaunchingWithOptions:

仍然在所有 Apple 文档链接中使用。

整个 Apple 文档都使用此约定。