我对AppleScript引用感到困惑......我几乎从不在AppleScript中开发,并且在AppleScript如何处理引用时遇到了很难找到的好文档。同样的代码,但是我运行它run
处理程序中,而不是foo
处理程序 -为什么AppleScript不能在此测试代码中将hash的firstValue转换为类型引用?
on run
foo()
end run
on foo()
set the hash to {firstValue:1, secondValue:2}
set the hashRef to a reference to the hash
return the firstValue of hashRef
end foo
但是,下面的代码工作::为什么
on run
set the hash to {firstValue:1, secondValue:2}
set the hashRef to a reference to the hash
return the firstValue of hashRef
end run
下面的代码失败,因为AppleScript的Can’t make firstValue of hash into type reference.
第一个代码示例在第二个代码示例工作时失败?更好的问题,有人可以指示我解释这个文件的方向,所以我可以知道我的错误是什么?
编辑:菲利普的答案指出我正确的方向,现在我看到了什么使我困惑。 The official AppleScript docs指出“AppleScript通过引用传递所有参数,这意味着处理程序和调用者之间共享一个传递的变量,就好像该处理程序使用set命令创建了一个变量”。 但是,这并不意味着AppleScript传递一个AppleScript参考对象作为每个参数!
这里的下面,更详细的,代码示例,展示我公司开发的最终工作的解决方案:
on run
foo()
end run
on isRef(someValue)
try
someValue as reference
return true
on error
return false
end try
end isRef
on foo()
log "In foo()"
set the objectList to makeObjectList()
log "objectList isRef =" & isRef(objectList)
set theObject to makeObject given id:0, name:"Test"
addObjectToObjectList(theObject, objectList)
log "foo(): object name =" & name of theObject
log item 1 of allItems of objectList
log item 1 of goodItems of objectList
set the name of item 1 of allItems of objectList to "Test3"
log item 1 of allItems of objectList
log item 1 of goodItems of objectList
end foo
on makeObjectList()
set the hash to {allItems:{}, goodItems:{}, badItems:{}}
return the hash
end makeObjectList
on makeObject given name:theName, id:theId
set theObject to {name:theName, id:theId}
return theObject
end makeObject
on addObjectToObjectList(object, objectList)
log "In addObjectToObjectList"
log "object isRef =" & isRef(object)
copy object to the end of allItems of the objectList
set objectRef to a reference to the last item in allItems of the objectList
set name of objectRef to "Test2"
log "object name =" & name of object
log "objectRef isRef =" & isRef(objectRef)
log "objectRef name =" & name of (contents of objectRef)
copy objectRef to the end of goodItems of the objectList
end addObjectToObjectList
,它的输出如下:
(*In foo()*) (*objectList isRef =false*) (*In addObjectToObjectList*) (*object isRef =false*) (*object name =Test*) (*objectRef isRef =true*) (*objectRef name =Test2*) (*foo(): object name =Test*) (*name:Test2, id:0*) (*name:Test2, id:0*) (*name:Test3, id:0*) (*name:Test3, id:0*)
问题的关键是,我不能在处理程序中引用局部变量 - 但只要这些引用存储回该记录中,我就可以引用记录的某些部分,这是我之后的功能。
我怀疑任何人都不会读到这里,下到这个问题:-)
你想要做什么,需要参考?我只问,因为我一直在Applescript工作多年,而且我从来没有需要参考,特别是在如此简单的情况下。您不需要创建对记录的引用来访问请求的值。我对引用的基本理解是,它与指针类似。 – 2010-08-25 20:26:52
@Philip - 我不认为我需要引用,但我的代码似乎没有工作没有他们...我(试图)做的是在内存中维护一个复杂的数据结构,并将它传递给各种处理程序来处理它。我还读到使用引用速度要快得多... – Josh 2010-08-25 20:28:45
@Philip:请参阅[Apple's Docs](http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html# // apple_ref/doc/uid/TP40000983-CH1g-BBCDBHIE),特别是关于“对于大型列表,在将大量项目插入列表时使用对运算符的引用更有效”的部分。我会看看我是否可以让我的代码无w/o裁判工作。 – Josh 2010-08-25 20:37:04