你如何有效地建立在一个AppleScript的处理程序中的列表吗? [英] How do you efficiently build a list within a handler in AppleScript?

查看:100
本文介绍了你如何有效地建立在一个AppleScript的处理程序中的列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppleScript的文档建议以下code高效地建立一个列表:

AppleScript documentation suggests the following code to efficiently build a list:

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

请注意使用一个明确的参考。这个工作在一个脚本或一个明确的运行处理程序中的顶级罚款,但如果你运行另一个处理程序中完全相同的code逐字像这样:

Note the use of an explicit reference. This works fine at the top level of a script or within an explicit run handler, but if you run the same exact code verbatim within another handler like so:

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入式的参考。为什么这休息了,还有什么比运行其他的处​​理程序中有效地建立一个列表的正确方法()?

it breaks, yielding an error message saying, "Can't make bigList into type reference." Why does this break, and what is the correct way to efficiently build a list within a handler other than run()?

推荐答案

添加全球bigList来buildList的第一行()修复了编译器错误。看来,运行处理程序中,变量默认情况下赤身露体,运营商的参考是非常有用的。然而,在其他情况下,变量已经基本上间接引用,并创建另一个参考层断裂的东西。声明在这些上下文全局变量剥离间接引用,并允许操作者的工作,以引用,但是这是不必要的。只要使用默认的间接引用。

Adding "global bigList" to the first line of buildList() fixes the compiler error. It seems that within the run handler, variables are naked by default, and the "a reference to" operator is useful. However, in other contexts, variables are already essentially indirect references, and creating another reference layer breaks stuff. Declaring a global variable in these contexts strips the indirect reference and allows the "a reference to" operator to work, but this is unnecessary. Just use the default indirect reference.

如果你不清楚这个,是因为我不完全理解的机制。如果你有什么是怎么回事更好的把握,请评论如下。

If this is unclear, it's because I don't completely understand the mechanism. If you have a better grasp of what's going on here, please comment below.

这篇关于你如何有效地建立在一个AppleScript的处理程序中的列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆