我收到一个错误,表示“丢失参数标签”日期:“在通话”根据这个屏幕截图:
这是代码: –
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中编程时,我从来没有意识到这种差异。
> Swift的功能和方法有什么区别?
>我们什么时候使用函数,什么时候我们在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,today is \(day)." } //Use Hash symbol to make the First parameter as external parameter func greet2 (#name: String,today is \(day)." } }
我可能会想念一些重要的细节。希望有人能提供更好的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。