AppleScript 文档建议使用以下代码来有效地构建列表:
set bigList to {}
set bigListRef to a reference to bigList
set numItems to 100000
set t to (time of (current date)) --Start timing operations
repeat with n from 1 to numItems
copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing
注意显式引用的使用。这在脚本的顶层或显式运行处理程序中运行良好,但如果您在另一个处理程序中逐字运行相同的确切代码,如下所示:
on buildList()
set bigList to {}
set bigListRef to a reference to bigList
set numItems to 100000
set t to (time of (current date)) --Start timing operations
repeat with n from 1 to numItems
copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing
end buildList
buildList()
它中断,产生一条错误消息,“无法将 bigList 转换为类型引用”。为什么这会中断,在 run() 之外的处理程序中有效构建列表的正确方法是什么?