【问题标题】:How to correct Expected type error? [duplicate]如何纠正预期类型错误? [复制]
【发布时间】:2015-09-27 23:35:41
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    Expected type
    

    如果您遇到这样的错误,只需检查苹果提供的有关此方法的文档,并检查您设置的所有内容是否正确。

    touchesBegan-documentation

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

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

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

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

    【讨论】:

      【解决方案2】:

      touchesBegan 方法需要 touches 作为参数,在您的代码的情况下,它只是 Set&lt;&gt;。预计会看到类似NSObjectSet。以下是它可以做的一个例子:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        相关资源
        最近更新 更多