简体   繁体   中英

How to use Vector in Flex?

I am trying to use the new Vector class introduced in Flash 10. Tried compiling the code using mxmlc but am getting this error message:

Error: Type was not found or was not a compile-time constant: Vector.

public var selectedRoutes:Vector.<Route>;
                      ^
  1. What could be the problem?
  2. What is the general consensus about the viability of using this feature?
  3. Can you do introspection of the Vector with describeType and get the type the Vector contains at runtime?

At a guess, you are compiling for FP9 instead of FP10, or perhaps against an older version of the SDK. Make sure everything is up to date.

The Vector type is more performant than using an Array and casting entries to the desired type.

I can't help feeling that they had to hack the compiler to make it work though. The generic-style syntax is valid only for the Vector type, and not a general language feature, which sucks.

If you need the performance or like the type-safety of using the Vector class then it's worth it.

I don't know what describeType gives you back on a Vector type. Why don't you give it a whirl?

I tried a describeType on a Vector declaration:

trace(describeType(Vector.<Route>))

The result was:

<type name=" AS3 .vec::Vector.&lt;components::Route>" base="Class" isDynamic="true" isFinal="false" isStatic="true">
<extendsClass type="Class"/>
<extendsClass type="Object"/>
<accessor name="prototype" access="readonly" type=" " declaredBy="Class"/>
<factory type=" AS3 .vec::Vector.&lt;components::Route>">
<extendsClass type=" AS3 .vec::Vector.&lt; >"/>
<extendsClass type="Object"/>
<accessor name="fixed" access="readwrite" type="Boolean" declaredBy=" AS3 .vec::Vector.&lt; >"/>
<accessor name="length" access="readwrite" type="uint" declaredBy=" AS3 .vec::Vector.&lt; >"/>
</factory>
</type>

So, yes, it does provide the type information. You can pick it out from either the type tag's name or from the factory tag's type.

You should open flex-config.xml and change the target player:

target-player>10.0.0

That will solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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