【问题标题】:Calling one method and getting result in different method in Applescript在 Applescript 中调用一种方法并以不同的方法获得结果
【发布时间】:2012-03-15 16:02:33
【问题描述】:

我试图返回在一种方法中声明的变量的值,并在另一种方法中获取该结果(从它被调用的地方)。我是一名新程序员,并且已经查看了 Apple 网站上的范围界定文件。我在下面包含了一些(编造的)代码,以显示我正在尝试做的事情。我很感激我能得到的任何帮助。提前谢谢你。

on first_method()
    set bob to "This is the variable I wish to return"
    return bob
end first_method

on second_method()
    first_method()
end second_method

如果我只是调用 first_method()(从 second_method 之外),它可以正常工作。但是我无法从 second_method 中获得要返回的值。我知道它是一个局部变量。但是我认为可以返回最后一个参数的结果并将其传递回最初调用它的位置。这是不正确的吗?有什么办法可以做我想做的事吗?

我意识到这可能真的很容易,我只是在证明我自己无法找到答案的愚蠢。但我真的很感激我在看到答案时能得到的任何帮助。我承诺有一天会在我更有经验的时候报答这份恩情。

谢谢。

【问题讨论】:

  • 您的脚本 sn-p 对我有用 - second_method() 确实返回 first_method() 调用的结果(尽管您通常会使用显式返回语句以避免误解)。或者你是在考虑变量“bob”而不是它的内容?

标签: variables methods scope applescript


【解决方案1】:

这样的?你的代码完美无缺。您返回的值需要分配给一个变量,这可能是您忽略的事情。

second_method()

on first_method()
    set bob to "This is the variable I wish to return"
    return bob
end first_method

on second_method()
    set bob to first_method()
    display dialog bob --just to show you that it works
end second_method

【讨论】:

  • 非常感谢。我警告过你我是菜鸟!我非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多