Swift:NSMutableSet - > Type'CGPoint'不符合协议'AnyObject'

use*_*836 1 typing nsmutableset swift

为什么这个代码结果为"Type'CGPoint'不符合协议'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))
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*ook 8

NSMutableSet只接受引用类型,但它CGPoint是一个结构,一个值类型.您可以将该点包装在一个中NSValue以添加它.

mutableSet.addObject(NSValue(CGPoint: touch.locationInNode(self)))
Run Code Online (Sandbox Code Playgroud)