23

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

Willie
  • 231
  • 1
  • 2
  • 3
  • Duplicate of http://stackoverflow.com/questions/28771896/overriding-method-with-selector-touchesbeganwithevent-has-incompatible-type ? – Martin R Apr 12 '15 at 00:01
  • 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, withEvent event: UIEvent) { if let touch = touches.first as? UITouch {" returns the error 'Set does not have a member named 'first". That's why it appeared it has something to do with classifying as an array? – Willie Apr 12 '15 at 00:13
  • Strange, the code compiles in my Xcode 6.3 project. – Martin R Apr 12 '15 at 00:17
  • 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. – Willie Apr 12 '15 at 00:23

2 Answers2

21
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

milo526
  • 5,012
  • 5
  • 41
  • 60
Weihong Chen
  • 311
  • 1
  • 4
  • Please explain by adding some more code or reference links to improve the answer. – Polynomial Proton Apr 12 '15 at 00:14
  • This is the reference https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIResponder_Class/index.html#//apple_ref/occ/instm/UIResponder/touchesBegan:withEvent: – Weihong Chen Apr 12 '15 at 00:21
  • 2
    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 – Polynomial Proton Apr 12 '15 at 00:23
3

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)
        }
    }
}