[英]How to initialize a typed array with values?
Assemblyscript initializes typed arrays with their length, like this: Assemblyscript 用它们的长度初始化类型化的 arrays,如下所示:
var arr = new Uint8Array(2);
Suppose I want to seed this array with some values, like [12, -47, 91], how would I do it?假设我想用一些值作为这个数组的种子,比如 [12, -47, 91],我该怎么做?
I found the answer myself.我自己找到了答案。 One must use StaticArray
like this:必须像这样使用StaticArray
:
var arr:StaticArray<i8> = [12, -47, 91];
You can use this approach if you need typed array instead StaticArray
如果您需要类型化数组而不是StaticArray
,则可以使用此方法
const arr = new Uint8Array(3);
arr.set([12, -47, 91]);
Ignore Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferView'.
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBufferView'.
warning for now.暂时警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.