简体   繁体   English

关于动态的Haxe迭代

[英]Haxe iteration on Dynamic

I have a variable of type Dynamic and I know for sure one of its fields, lets call it a , actually is an array. 我有一个Dynamic类型的变量,我肯定知道它的a字段,我们称之为a ,实际上是一个数组。 But when I'm writing 但是当我写作的时候

var d : Dynamic = getDynamic();
for (t in d.a) {
}

I get a compilation error on line two: 我在第二行得到一个编译错误:

You can't iterate on a Dynamic value, please specify Iterator or Iterable 您无法迭代动态值,请指定Iterator或Iterable

How can I make this compilable? 我怎样才能使这个可编辑?

Haxe can't iterate over Dynamic variables (as the compiler says). Haxe无法迭代Dynamic变量(正如编译器所说)。

You can make it work in several ways, where this one is probably easiest (depending on your situation): 您可以通过多种方式使其工作,这可能是最简单的(取决于您的情况):

var d : {a:Array<Dynamic>} = getDynamic();
for (t in d.a) { ... }

You could also change Dynamic to the type of the contents of the array. 您还可以将Dynamic更改为数组内容的类型。

Another way to do the same is to use an extra temp variable and explicit typing: 另一种方法是使用额外的临时变量和显式输入:

var d = getDynamic();
var a: Array<Dynamic> = d.a;
for (t in a) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM