user3673836
user3673836

Reputation: 591

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

Upvotes: 1

Views: 1565

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

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

Upvotes: 8

Related Questions