Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 不同的函数如何具有相同的名称?_Swift_Cocoa - Fatal编程技术网

Swift 不同的函数如何具有相同的名称?

Swift 不同的函数如何具有相同的名称?,swift,cocoa,Swift,Cocoa,发件人: 响应位置事件 func locationManager(CLLocationManager, didUpdateLocations: [CLLocation]) 通知代理新位置数据可用 func locationManager(CLLocationManager, didFailWithError: Error) 告诉代理位置管理器无法检索 位置值 func locationManager(CLLocationManager, didFinishDeferredUpdatesWit

发件人:

响应位置事件

func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])
通知代理新位置数据可用

func locationManager(CLLocationManager, didFailWithError: Error)
告诉代理位置管理器无法检索 位置值

func locationManager(CLLocationManager, didFinishDeferredUpdatesWithError: Error?)
通知代理更新将不再延迟

func locationManager(CLLocationManager, didUpdateTo: CLLocation, from: CLLocation)
告诉代理新的位置值可用

func locationManager(CLLocationManager, didFailWithError: Error)

我有一段代码如下所示:

ViewController.swift
我怎么能在同一个类中有两个同名但根据传递的参数以独立方式调用的函数?在其他编程语言中,恐怕不能这样做。

在Swift中,参数名和类型是函数名的一部分。因此,在您的示例中,函数的命名不同,因为参数不同

这就是为什么在中的方法名称中包含参数名称,例如:

locationManager(_:didUpdateLocations:)
locationManager(_:didFailWithError:)
locationManager(_:didFinishDeferredUpdatesWithError:)
此外,即使参数的名称相同,如果它们的类型不同,这也是允许的。一个简单的例子:

class Greeter {
    func greet(arg: Int) -> String {
        if (arg < 12) {
            return "Good morning!"
        } else if (arg < 18) {
            return "Good afternoon!"
        } else {
            return "Good evening!"
        }
    }

    func greet(arg: String) -> String {
        return "Hello, \(arg)."
    }
}
类迎宾员{
func问候语(arg:Int)->字符串{
如果(arg<12){
回答“早上好!”
}否则如果(arg<18){
回答“下午好!”
}否则{
回答“晚上好!”
}
}
func问候语(arg:String)->String{
返回“Hello,\(arg)”
}
}
在本例中,您可以调用
Greeter().greet(4)
Greeter().greet(“Aaron”)
并将控件流到相应的函数。

它被称为“”(或“方法重载”),具有多个同名函数/方法,这些函数/方法通过其签名(参数的数量和类型)进行区分。它在编程语言中非常普遍,包括C++、爪哇、C语言以及显然的SWIFT。 如何在封面下处理重载因语言而异。例如,我最近看了(很久以前),C++使用了名字的模糊——函数的实际名称是通过使用基名称并添加到它来表示参数的类型来创建的。(很早以前,C++是一个输出C代码的预处理器。)java不需要这么做,因为它是用超载来构建的。但是在我们处理它们的高层,函数具有相同的名称。

函数重载。 C++,可以做到。< /P>
编辑:

嗯。我补充了更多细节。对不起,我是编程新手。我害怕给出错误的答案

func foo1(_ x: Int) {
    print("the argument is Int")
}
func foo1(_ x: Double) {
    print("the argument is Double")
}

// it depends the type of the value to pass to the function.
foo1(3) // print "the argument is Int"
foo1(4.7) // print "the argument is Double"

// when you assign the function to a variable,
// and there are other functions with the same name and same parameter names, 
// then, you have to tell the type of the function explicitly.
let pToFunc1: (Int) -> Void = foo1

// but normally, you can assign it without telling the type.
func foo2(x: Int){}
let pToFunc2 = foo2

// but same function name with different parameter names,
// when you assign this function, you need to tell the parameter explicitly.  
func foo3(x: Int){}
foo3(x: 4)
func foo3(y: Int){}
foo3(y: 5)
let pToFunc3 = foo3(x:)

这也包括参数类型吗?所以在你提供的例子中,我猜“u:did…”部分是识别函数的部分,对吗?两者都是。我更新了一个例子。如果参数和类型相同,但输出/内容不同,会发生什么情况?将调用哪个函数?是否会出现运行时错误?如果参数名称相同且参数类型相同,代码将不会编译。如果类型不同(例如String和Int),函数将根据您传入的类型被调用。这个答案没有添加任何以前没有说过的内容,而且也非常模糊。你应该考虑删除它或添加一些额外的信息来丰富答案。好的解释。我不知道你说的最后一部分。请投票给我,很遗憾我试图回答,但投票给了一个新手(我)。这是我第一次或第二次尝试回答。我知道,如果我不删除,你还有3个下载,这会影响我的个人资料吗?这对我来说很有趣,因为我来自Python,既没有学习过C++,也没有学习过java,也没有学习过Swift,而且它对我来说真的很惊讶。在使用动态或松散类型(如Python或JavaScript)的语言中,不能进行重载。