小编典典

Swift中的函数和方法之间有什么区别?

swift

我一直以为功能和方法是相同的,直到我通过“ Swift编程语言”电子书学习Swift 之前。我发现我
不能greet(“John”, “Tuesday”)用来调用在类中声明的函数,

Here is the code:-

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)."
    }
}

当我在Objective-C中编程时,我从未意识到这种差异。

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

  2. 在Swift中什么时候使用函数以及何时使用方法?


阅读 387

收藏
2020-07-07

共1个答案

小编典典

经过几个小时的阅读和实验,以下是我发现的内容:

Swift中的函数

函数是执行特定任务的独立代码块。您为函数指定一个名称,该名称可以标识其功能,该名称为“调用”该函数以在需要时执行其任务。

资源
有关Swift中功能的Apple官方文档

功能参数名称

但是,这些参数名称仅在函数本身的主体内使用,而在调用函数时不能使用。这些类型的参数名称称为局部参数名称,因为它们仅可在函数体内使用。

这意味着默认情况下,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.

Resource : 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.

Code below shows the differences for default and non-default parameters for
method in 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)."
    }
}

I might miss some important details. Hope someone can provide a better answer.

2020-07-07