“Set<NSObject>”没有名为“anyObject”的成员。” - Xcode 6.3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29584365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Set<NSObject>' does not have a member named 'anyObject." - Xcode 6.3
提问by Willie
I'm checking to see if an element has been selected.
我正在检查是否选择了一个元素。
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
// First, see if the game is in a paused state
if !gamePaused
{
// Declare the touched symbol and its location on the screen
let touch = touches.anyObject! as? UITouch
let location = touch.locationInNode(symbolsLayer)
And this had previously compiled fine in Xcode 6.2 but with a 6.3 update, the line "let touch = touches.anyObject! as? UITouch" is throwing the error:
这之前在 Xcode 6.2 中编译得很好,但在 6.3 更新后,“let touch = touches.anyObject!as? UITouch”这一行抛出了错误:
'Set' does not have a member named 'anyObject'
“Set”没有名为“anyObject”的成员
I've read through many similar question, but I can't seem to wrap my head around "To use the value, you need to “unwrap” it first." Especially because the answers seem to focus on notifications.
我已经阅读了许多类似的问题,但我似乎无法理解“要使用该值,您需要先“解开”它。特别是因为答案似乎集中在通知上。
Thank you so much. W
非常感谢。宽
回答by Weihong Chen
let touch = touches.first as? UITouch
.first
can allow you to access first object of UITouch.
.first
可以让你访问 UITouch 的第一个对象。
Since Xcode 6.3 uses an updated version of Swift (1.2) you need to convert your old code into Swift 1.2 (Edit -> convert -> To lastest Swift).
由于 Xcode 6.3 使用 Swift (1.2) 的更新版本,因此您需要将旧代码转换为 Swift 1.2(编辑 -> 转换 -> 最新的 Swift)。
Swift 1.2, uses Set
's (new in Swift) instead of using NSSet
's (old one in Objective-C). Thus the touchbegan function also changes its parameters from NSSet to Set.
Swift 1.2,使用Set
's(Swift 中的新)而不是使用NSSet
's(Objective-C 中的旧)。因此,touchbegan 函数也将其参数从 NSSet 更改为 Set。
For more info, refer this
有关更多信息,请参阅此
回答by martin meincke
This would check for multiple touches in symbolsLayer
这将检查符号层中的多次触摸
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
// First, see if the game is in a paused state
if !gamePaused
{
// Declare the touched symbol and its location on the screen
for touch: AnyObject in touches {
let location = (touch as! UITouch).locationInNode(symbolsLayer)
}
}
}