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

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

问题描述

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





我收到一个错误消息,指出缺少参数标签'日:致电





以下是代码: -

  import Foundation 
导入UIKit

类ViewController2 :UIViewController {
覆盖func viewDidLoad(){
super.viewDidLoad()

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

func greet(name:String,day:String) - >字符串{
returnHello \(name),今天是\(day)。


$ / code>

经过一番调查,我找到了这篇文章:方法和函数之间的区别,它在我看来,我在类中声明的函数实际上称为方法。因此,与用于调用函数的语法相比,我用来调用方法的语法不同。

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


  1. Swift中的函数和方法有什么不同?


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

    经过几个小时的阅读和试​​验之后,我发现了以下内容:



    Swift中的函数


    函数是自包含的代码块,用于执行特定的
    任务。你给一个函数一个名字来标识它做了什么,
    这个名字用于在需要
    时调用函数来执行它的任务。


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



    函数参数名称 b
    $ b


    然而,这些参数名称仅用于
    函数本身的内部,并且不能使用当调用该函数时。这些
    类型的参数名称被称为本地参数名称,因为
    它们只能在函数体内使用。


    这意味着默认情况下,Function的所有参数都是本地参数。但是,有时候我们想指出每个参数的用途。所以,我们实际上可以为每个参数定义一个外部参数名称。示例代码:

      func someFunction(externalParameterName localParameterName:Int){
    //函数体在这里,可以使用localParameterName
    //引用该参数的参数值
    }

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

      func someFunction(#localParameterName:Int){
    //函数体在这里,可以使用localParameterName
    //引用该参数的参数值
    }

    调用你可以使用

      someFunction(localParameterName:10)


    $ b

    Swift中的方法
    $ b


    方法是与特定类型相关的函数。
    类,结构和枚举都可以定义实例方法,
    封装了特定任务和功能,用于处理给定类型的
    实例。


    blockquote>

    资源苹果官方有关Swift中方法的文档


    然而,对于函数和方法,局部名称和外部名称的默认行为是



    具体来说,Swift给出了第一个参数在默认情况下,在方法中命名一个本地
    参数名称
    ,并且默认为第二个和后续的
    参数名称指定本地和外部参数名称。


    < blockquote>

    下面的代码显示了m的默认和非默认参数的差异

      import Foundation 
    导入UIKit

    类ViewController2:UIViewController {
    重写func viewDidLoad(){
    super.viewDidLoad()

    //调用
    的默认方法var dailyStatement = greet(Rick,day:Tuesday)
    println(dailyStatement)

    //第一个参数也是一个外部参数
    var dailyStatement2 = greet2(name:John,day:Sunday)
    println(dailyStatement2)
    }

    //默认值:第一个参数是本地参数,其余的是外部参数
    func greet(name:String,day:String) - > ;字符串{
    returnHello \(name),今天是\(day)。
    }

    //使用哈希符号使第一个参数成为外部参数
    func greet2(#name:String,day:String) - >字符串{
    returnHello \(name),今天是\(day)。
    }
    }

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


    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:

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

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

    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.

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

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

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

    Functions in 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.

    Resource: Official Apple Documentation on Functions in Swift

    Function Parameter Names

    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.

    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)
    

    Methods in 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.

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

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