'Set<NSObject>' does not have a member named 'anyObject." - Xcode 6.3

10,344

Solution 1

let touch =  touches.first as? UITouch

.first can allow you to access first object of UITouch.

Since Xcode 6.3 uses an updated version of Swift (1.2) you need to convert your old code into Swift 1.2 (Edit -> convert -> To lastest Swift).

Swift 1.2, uses Set’s (new in Swift) instead of using NSSet’s (old one in Objective-C). Thus the touchbegan function also changes its parameters from NSSet to Set.

For more info, refer this

Solution 2

This would check for multiple touches in symbolsLayer

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)
        }
    }
}
Share:
10,344

Related videos on Youtube

Willie
Author by

Willie

Updated on July 23, 2022

Comments

  • Willie
    Willie almost 2 years

    I'm checking to see if an element has been selected.

    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)
    

    And this had previously compiled fine in Xcode 6.2 but with a 6.3 update, the line "let touch = touches.anyObject! as? UITouch" is throwing the error:

    'Set' does not have a member named 'anyObject'

    I've read through many similar question, but I can't seem to wrap my head around "To use the value, you need to “unwrap” it first." Especially because the answers seem to focus on notifications.

    Thank you so much. W

    • Martin R
      Martin R about 9 years
    • Willie
      Willie about 9 years
      Thanks Martin R. I have tried that but it appears to not quite be what is the issue, changing to what you suggested "override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch {" returns the error 'Set<NSObject> does not have a member named 'first". That's why it appeared it has something to do with classifying as an array?
    • Martin R
      Martin R about 9 years
      Strange, the code compiles in my Xcode 6.3 project.
    • Willie
      Willie about 9 years
      I am too much of a beginner to understand why that could be the case. Any coding I do is riddled with errors. Thank you so much for trying.
  • Polynomial Proton
    Polynomial Proton about 9 years
    Please explain by adding some more code or reference links to improve the answer.
  • Weihong Chen
    Weihong Chen about 9 years
  • Willie
    Willie about 9 years
    This seems to have solved it! Thank you so much Martin!
  • Polynomial Proton
    Polynomial Proton about 9 years
    Thanks. Can you edit your answer and add the same? and explain more, so everyone understands. SO is not meant to share links ONLY, but explain and give a reference link