Swift: NSMutableSet -> Type 'CGPoint' does not conform to protocol 'AnyObject'

1.5k views Asked by At

Why does this code results to "Type 'CGPoint' does not conform to protocol 'AnyObject'"?

let mutableSet = NSMutableSet()
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    touch = touches.anyObject() as UITouch!
    mutableSet.addObject(touch.locationInNode(self))
}
1

There are 1 answers

0
Nate Cook On BEST ANSWER

NSMutableSet only accepts reference types, but CGPoint is a struct, a value type. You can wrap the point in an NSValue to add it.

mutableSet.addObject(NSValue(CGPoint: touch.locationInNode(self)))