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

时间:2014-11-12 14:45:15

标签: swift typing nsmutableset

为什么这个代码结果为“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))
}

1 个答案:

答案 0 :(得分:8)

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

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