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

Ios 如何更正预期的类型错误?,ios,swift,Ios,Swift,我跟随一篇文章编写代码。它给了我这个预期的类型错误 这是我的代码: override func touchesBegan(touches: Set<>, withEvent event: UIEvent) { startUpdateLoop() animateControlPoints() } override func touchsbegined(touchs:Set,withEvent-event:UIEvent){ startUpdateLoop() anim

我跟随一篇文章编写代码。它给了我这个预期的类型错误

这是我的代码:

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

touchesbeated
方法期望
touches
作为一个参数,在您的代码中,该参数仅为
设置
。它希望看到一个类似于
NSObject
集合。下面是一个例子,说明它可以做些什么,除了:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {...}
override func touchsbegind(touchs:Set,withEvent-event:UIEvent){…}
如果您使用的是Swift 2和Xcode 7,那么您可能会注意到该覆盖中的差异

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

是指向此方法工作原理的链接。

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

Expected type
如果您遇到这样的错误,只需检查苹果公司为该方法提供的文档,并检查您设置的所有内容是否正确

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

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