Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
Ios 为什么swift中存在选择器_Ios_Swift_Selector - Fatal编程技术网

Ios 为什么swift中存在选择器

Ios 为什么swift中存在选择器,ios,swift,selector,Ios,Swift,Selector,为什么苹果选择继续使用选择器类型,而Swift完全能够发送该功能 因此,与其这样做,不如: UILongPressGestureRecognizer(target: self, action: "showSomething:") 我们可以做到: UILongPressGestureRecognizer(showSomething) 如果您有代表,这也适用: UILongPressGestureRecognizer(delegate.showSomething) 苹果选择保留字符串的sad实

为什么苹果选择继续使用
选择器
类型,而Swift完全能够发送该功能

因此,与其这样做,不如:

UILongPressGestureRecognizer(target: self, action: "showSomething:")
我们可以做到:

UILongPressGestureRecognizer(showSomething)
如果您有代表,这也适用:

UILongPressGestureRecognizer(delegate.showSomething)
苹果选择保留字符串的sad实现来指向函数,而不是最大限度地使用Swift的功能,这有什么原因吗

编辑:示例实现
showSomething

func showSomething(sender: UIGestureRecognizer) {
    println("showSomething triggered")
}

这是一个遗留兼容性问题

您用作示例的API是用Objective-C编写的,现在仍然是。苹果完全有可能利用Swift的功能来生产更精简的API,但这需要在Swift中重新编写它们,这对于构成Cocoa&Cocoa Touch的大量现有框架来说需要花费大量的时间和精力

苹果可能会向Objective-C添加一些功能,通过利用较新的Swift功能来提高语言之间的兼容性,同时避免重写框架。例如,在Xcode 6.3 beta版中,他们在Objective-C中添加了“可空性”的概念(参见发行说明),这会影响Objective-C API在Swift代码中使用时的可选性。Swift可以改进现有API的另一个例子是使用默认参数。。。也许这也可以在Objective-CAPI中描述


看到苹果将来发布非Objective-C(原生Swift)框架,我不会感到惊讶,但我们离这还有一点距离。

只有苹果知道。但是允许字符串指定一个方法是有优势的,因为可以动态创建类。为了与Objective-C兼容,我猜。请注意,
(target:self,action:“showSomething:”)
中的选择器与
(showSomething)
不同。最后一个“:”表示需要一个参数。@Zaph我知道,但是Swift可以知道当您给出函数本身时需要什么参数。@Zaph问题代码非常好,我将添加一个
showSomething
的示例实现来说明我的意思。