开发者

In ActionScript, Is there a way to check if an input argument is a valid Vector of any type?

In the following code:

var a:Vector.<int> ...
var b:Vector.<String> ...
var c:Vector.<uint> ...
var c:Vector.<MyOwnClass> ...

function verifyArrayLike(arr:*):Boolean
{
   开发者_如何学Pythonreturn (arr is Array || arr is Vector)
}

verifyArrayLike(a);
verifyArrayLike(b);
...

What I'm looking for is something like _var is Vector.<*>

But Vector.<*> is not a valid expression, even Vector. can not be placed at the right side of operators.

Is there a way to check if an input argument is a valid Vector of any type?


Here's a method that should work. I'm confident there must (surely?) be a better way out there that doesn't use strings, but this method should tide you over.

/**
 * Finds out if an object is a generic Vector.
 * It works because the value returned for getQualifiedClassName(a vector) 
 * is "__AS3__.vec::Vector.<the vector's type>".
 * @param object Object Any object.
 * @return Boolean True if the object is a generic Vector, false otherwise.
 */
function isVector(object:Object):Boolean 
{
    var class_name:String = getQualifiedClassName(object);
    return class_name.indexOf("__AS3__.vec::Vector.") === 0;
}


This also seems to work, although I'm very unhappy about not being able to use (candidate is Vector) reliably.

private function isVector(candidate : *) : Boolean
{
    var result : Boolean;
    try
    {
        Vector.<*>(candidate).length;
        result = true;
    }
    catch (error : Error)
    {
        result = false;
    }

    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜