如何更正预期的类型错误?

时间:2015-09-27 23:35:42

标签: ios swift

我已经按照一篇文章来编写代码。它给了我这个预期的类型错误。

这是我的代码:

override func touchesBegan(touches: Set<>, withEvent event: UIEvent) {
    startUpdateLoop()
    animateControlPoints()
}

2 个答案:

答案 0 :(得分:0)

touchesBegan方法需要touches作为参数,在您的代码中只有Set<>。期待看到像Set这样的NSObject。这是一个例子,除了:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {...}

如果您使用的是Swift 2和Xcode 7,那么您可能会注意到此覆盖的差异。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {...}

Here是指向此方法的工作方式的链接。

答案 1 :(得分:0)

让我们先检查错误。它为您提供所需的信息:

Expected type

如果您收到此类错误,请查看Apple为此方法提供的文档,并检查您设置的所有内容是否正确。

touchesBegan-documentation

如您所见,文档显示,您的实现与Apple提供的实现不同:

func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)

如您所见,您需要将Set的类型设置为UITouch。目前此值为空:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
    startUpdateLoop()
    animateControlPoints()
}