AppleScript的文檔建議將以下代碼有效地建立一個列表:如何在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()以外的處理程序中有效地構建列表的正確方法是什麼?
這裏遇到了同樣的問題(沒有滿意的答案):http://stackoverflow.com/questions/3569847/why-cant-applescript-make-firstvalue-of-hash-into-type-reference-in-this -test-c?rq = 1 – 108
更有幫助的是這個:http://stackoverflow.com/questions/15777321/why-in-applescript-cant-you-declare-references-to-variables-local-to-handlers – 108