Swift 中的函数和方法有什么区别? [英] What are the differences between functions and methods in Swift?

查看:14
本文介绍了Swift 中的函数和方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为函数和方法是一样的,直到我通过Swift Programming Language"电子书学习 Swift.我发现我不能使用 greet("John", "Tuesday") 来调用我在类中声明的函数,如下面的电子书截图所示:

I always thought functions and methods were the same, until I was learning Swift through the "Swift Programming Language" eBook. I found out that I cannot use greet("John", "Tuesday") to call a function that I declared inside a class, as shown in the eBook in the screen shot below:

我收到一条错误消息,指出调用中缺少参数标签‘day:’",如下面的屏幕截图所示:

I received a error saying that "Missing argument label 'day:' in call" as per this screen shot:

代码如下:-

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

经过一番研究,我发现了这篇文章:方法和函数的区别,在我看来,我在类中声明的函数实际上被称为方法.因此,与我用来调用函数的语法相比,我用来调用方法的语法是不同的.

After some research, I found this post: Difference between a method and a function, and it seems to me that the function that I declared inside a class is actually called a method. So, the syntax that I use to call the method is different compared to the syntax that I use to call a function.

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

I never realized this difference when I was programming in Objective-C.

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

  1. What are the differences between functions and methods in Swift?

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

When do we use functions and when do we use methods in Swift?

推荐答案

经过几个小时的阅读和实验,我发现了以下几点:-

After a few hours of reading and experimenting, here are the things that I found out:-

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.

资源:关于 Swift 函数的 Apple 官方文档

函数参数名称

然而,这些参数名称仅用于函数本身,不能在调用函数时使用.这些各种参数名称被称为局部参数名称,因为它们只能在函数体内使用.

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的所有参数默认都是局部参数.

It means that by default, all the parameters for Function are local parameters.

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

But, sometimes we want to indicate the purpose of each parameter. So, we can actually define an external parameter name for each parameter. Example Code:

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

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

Another way to make the external parameter name is using hash symbol (#) to shorten the name.

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

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

To call the above functions with external parameter, you may use

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.

资源:关于 Swift 方法的 Apple 官方文档

但是,本地名称和外部名称的默认行为是功能和方法不同.

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

具体来说,Swift 将方法中的第一个参数名称指定为本地默认参数名称,并给出第二个和后续参数名称默认本地和外部参数名称.

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 中方法的默认和非默认参数的区别.

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.

这篇关于Swift 中的函数和方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆