gpt4 book ai didi

Swift 链接选项

转载 作者:行者123 更新时间:2023-11-28 05:34:30 26 4
gpt4 key购买 nike

我正在运行 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)
}

最佳答案

#2、#3、#4的失败都是因为缺少类型。你说:

the error for #2 seems to usually apply to type casting, and I wouldn't think there is any type casting going on here.

但是,当您尝试将其作为 SCNNode 访问时,您将 result 声明为 AnyObject,因此肯定存在某种类型转换这需要发生。

我以前在使用字典时也见过这种情况。我不仅明确了类型,还预先测试了类型:

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
}

关于Swift 链接选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com