【问题标题】:'Set<NSObject>' does not have a member named 'anyObject." - Xcode 6.3“Set<NSObject>”没有名为“anyObject”的成员。” - Xcode 6.3
【发布时间】:2015-04-11 23:50:56
【问题描述】:

我正在检查是否选择了一个元素。

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
    // First, see if the game is in a paused state
    if !gamePaused
    {
        // Declare the touched symbol and its location on the screen
        let touch = touches.anyObject! as? UITouch
        let location = touch.locationInNode(symbolsLayer)

这之前在 Xcode 6.2 中编译得很好,但随着 6.3 的更新,“let touch = touches.anyObject! as? UITouch”这行会抛出错误:

“Set”没有名为“anyObject”的成员

我已经阅读了许多类似的问题,但我似乎无法理解“要使用该值,您需要先“解开”它。尤其是因为答案似乎集中在通知上。

非常感谢。 W

【问题讨论】:

  • 谢谢 Martin R。我已经尝试过了,但似乎不是问题所在,改为您建议的“覆盖 func touchesBegan(touches: Set, withEvent event: UIEvent) { if let touch = touches.first as? UITouch {" 返回错误 'Set 没有名为 'first" 的成员。这就是为什么它似乎与分类为数组有关?
  • 奇怪,代码在我的 Xcode 6.3 项目中编译。
  • 我太初学者了,无法理解为什么会这样。我所做的任何编码都充满了错误。非常感谢您的尝试。

标签: ios xcode swift set nsobject


【解决方案1】:
let touch =  touches.first as? UITouch

.first可以让你访问UITouch的第一个对象。

由于 Xcode 6.3 使用 Swift (1.2) 的更新版本,您需要将旧代码转换为 Swift 1.2(编辑 -> 转换 -> 到最新的 Swift)。

Swift 1.2,使用Set's(Swift 中的新功能)而不是使用NSSet's(Objective-C 中的旧功能)。因此,touchbegan 函数也将其参数从 NSSet 更改为 Set。

欲了解更多信息,refer this

【讨论】:

【解决方案2】:

这将检查symbolLayer中的多次触摸

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
    // First, see if the game is in a paused state
    if !gamePaused
    {
        // Declare the touched symbol and its location on the screen
        for touch: AnyObject in touches {
            let location = (touch as! UITouch).locationInNode(symbolsLayer)
        }
    }
}

【讨论】:

  • 这个好像解决了!非常感谢马丁!
猜你喜欢
  • 2014-12-17
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多