Search code examples
swifttypingnsmutableset

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


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

Solution

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