【发布时间】:2015-09-27 23:35:41
【问题描述】:
我已经按照一篇文章编写代码。它给了我这个预期的类型错误。
这是我的代码:
override func touchesBegan(touches: Set<>, withEvent event: UIEvent) {
startUpdateLoop()
animateControlPoints()
}
【问题讨论】:
我已经按照一篇文章编写代码。它给了我这个预期的类型错误。
这是我的代码:
override func touchesBegan(touches: Set<>, withEvent event: UIEvent) {
startUpdateLoop()
animateControlPoints()
}
【问题讨论】:
让我们先检查一下错误。它为您提供所需的信息:
Expected type
如果您遇到这样的错误,只需检查苹果提供的有关此方法的文档,并检查您设置的所有内容是否正确。
如您所见,文档显示,您的实现与苹果提供的不同:
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
如您所见,您需要将Set 的类型设置为UITouch。目前该值为空:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
startUpdateLoop()
animateControlPoints()
}
【讨论】:
touchesBegan 方法需要 touches 作为参数,在您的代码的情况下,它只是 Set<>。预计会看到类似NSObject 的Set。以下是它可以做的一个例子:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {...}
如果您使用的是 Swift 2 和 Xcode 7,那么您可能会注意到此覆盖的不同之处。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {...}
Here 是此方法工作原理的链接。
【讨论】: