我正在运行Yosemite公共测试版& Xcode6 Beta 4
基于OSX SceneKit模板,我试图确定点击了哪个节点。这是mouseDown函数,主要来自模板代码。
标记为#1的评论很有效,但是我试图理解为什么代码注释#2,#3和#4不能编译,或者错误是什么我
搜索错误我没有找到似乎适用于我的案例的结果。
#2的错误似乎通常适用于类型转换,我不认为这里有任何类型转换。
#3的错误让我完全迷失了。
并且#4的错误似乎SCNNode没有名称属性,但肯定会。
override func mouseDown(theEvent: NSEvent) {
/* Called when a mouse click occurs */
// check what nodes are clicked
let p = gameView.convertPoint(theEvent.locationInWindow, fromView: nil)
let hitResults = gameView.hitTest(p, options: nil)
// check that we clicked on at least one object
if (hitResults.count > 0){
// retrieved the first clicked object
let result: AnyObject = hitResults[0]
// #1 This works
if let myNode: SCNNode = result.node? {
if myNode.name? == "Die" {
println("Node is named Die")
}
}
// #2 This does not work
// error: Could not find an overload for the 'node' that accepts the supplied arguments
if let myNode = result.node? {
if myNode.name? == "Die" {
println("Node is named Die")
}
}
// #3 This does not work either
// error: Type 'String?' does not confrom to protocol '_RawOptionSet'
if result.node?.name? == "Die" {
println("Node is named Die")
}
// #4 This does not work either
// error: 'SCNNode!' does not have a member named 'name'
if let myName = result.node?.name? {
if myName == "Die" {
println("Node is named Die")
}
}
// get its material
let material = result.node!.geometry.firstMaterial;
// highlight it
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)
// on completion - unhighlight
SCNTransaction.setCompletionBlock() {
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)
material.emission.contents = NSColor.blackColor()
SCNTransaction.commit()
}
material.emission.contents = NSColor.redColor()
SCNTransaction.commit()
}
super.mouseDown(theEvent)
}
答案 0 :(得分:2)
#2,#3和#4的失败都是因为缺乏类型。你说:
#2的错误似乎通常适用于类型转换,我不认为这里有任何类型转换。
但是,当您尝试以result
形式访问AnyObject
时,您宣布SCNNode
为var item: AnyObject? = nil
item = map["SWLFlexFormat"]
if let value: AnyObject = item {
configuration.formatter = getConfiguredFlexFormatter(configuration, item: value);
}
func getConfiguredFlexFormatter(configuration: LoggerConfiguration, item: AnyObject) -> LogFormatter? {
if let formatString: String = item as? String {
var formatter = FlexFormatter.logFormatterForString(formatString);
return formatter
}
return nil
}
,因此当然需要进行某些类型转换发生。
我之前已经看过这个和词典一起工作了。我不仅明确了类型,我还预先测试了类型:
{{1}}