function - Swift 中的函数和方法有什么区别?

标签 function methods swift

我一直认为函数和方法是相同的,直到我通过“Swift 编程语言”电子书学习 Swift。我发现我无法使用 greet("John", "Tuesday") 调用我在类中声明的函数,如下面屏幕截图中的电子书所示:

function declaration in swift

根据此屏幕截图,我收到一条错误消息,指出“调用中缺少参数标签‘day:’”:

Error message in swift

这是代码:-

import Foundation
import UIKit

class ViewController2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        //var dailyStatement = greet("John", "Tuesday")
        var dailyStatement = greet("John", day: "Tuesday")
        println(dailyStatement)
    }

    func greet(name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }
}

经过一些研究,我找到了这个帖子:Difference between a method and a function ,在我看来,我在类中声明的函数实际上称为方法。因此,我用来调用该方法的语法与我用来调用函数的语法不同。

我在使用 Objective-C 编程时从未意识到这种差异。

  1. Swift 中的函数和方法有什么区别?

  2. 在 Swift 中我们什么时候使用函数,什么时候使用方法?

最佳答案

经过几个小时的阅读和试​​验,这里是我发现的东西:-

Swift 中的函数

Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

资源:Official Apple Documentation on Functions in Swift

函数参数名称

However, these parameter names are only used within the body of the function itself, and cannot be used when calling the function. These kinds of parameter names are known as local parameter names, because they are only available for use within the function’s body.

意思是默认情况下,Function的所有参数都是局部参数

但是,有时我们想指明每个参数的用途。因此,我们实际上可以为每个参数定义一个外部参数名称。示例代码:

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

另一种制作外部参数名称的方法是使用哈希符号 (#) 来缩短名称。

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

要使用外部参数调用上述函数,您可以使用

someFunction(localParameterName:10)

Swift 中的方法

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type.

资源:Official Apple Documentation on Methods in Swift

However, the default behavior of local names and external names is different for functions and methods.

Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default.

下面的代码显示了 Swift 中方法的默认参数和非默认参数的区别。

import Foundation
import UIKit

class ViewController2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        //Default methods calling
        var dailyStatement = greet("Rick", day: "Tuesday")
        println(dailyStatement)

        //First parameter is also an external parameter
        var dailyStatement2 = greet2(name:"John", day: "Sunday")
        println(dailyStatement2)
    }

    //Default: First Parameter is the local parameter, the rest are external parameters
    func greet (name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }

    //Use Hash symbol to make the First parameter as external parameter
    func greet2 (#name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }
}

我可能会错过一些重要的细节。希望有人能提供更好的答案。

关于function - Swift 中的函数和方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133879/

相关文章:

c - C 中赋值中的不兼容类型

java - 执行 map-reduce 操作的通用方法。 (Java-8)

Javascript:函数声明类型对于 setInterval 和 onComplete 很重要吗?

c++ - 为什么第一个程序不起作用但第二个程序有效?第二,为什么输出是它给出的?

go - 试图将文字转换为 Golang 中的指针

swift - 无法在扩展中设置关联对象的存储属性

go - 使用来自其父级的方法更改嵌入式结构的属性

c# - 如何在 C# 中的方法中返回多个值....这可能吗?

ios - 快速在选项卡式 View 之间传递数据

swift - 检查 OS X 上是否预装或保护应用程序