2012-04-18 78 views
0

我想使用ObjectUtil.compare()来比较两个字典。当字典是相同的(两个不同的实例,但具有相同的内容),比较失败,一个奇怪的例外:Flex:比较两个相同的字典与ObjectUtil失败?

Error #1034: Type Coercion failed: cannot convert "some_key" to QName. 

下面是一些代码,以使其更清晰。

Main.mxml

<?xml version="1.0" encoding="utf-8"?> 
<local:MainTest xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:local="*"/> 

MainTest.as

package { 
    import spark.components.Application; 
    import flash.utils.Dictionary; 
    import mx.utils.ObjectUtil; 

    public class MainTest extends Application { 
    public function MainTest() { 
     super(); 
     trace(ObjectUtil.compare(getMyDictionary(), getMyDictionary())); 
    } 

    private function getMyDictionary() : Dictionary { 
     var myDictionary : Dictionary = new Dictionary(); 
     myDictionary["oranges"] = "orange"; 
     myDictionary["kiwis"] = "green"; 
     return myDictionary; 
    } 
    } 
} 

正如你可以看到,构造函数调用ObjectUtil.compare有两个词典。 getMyDictionary()方法显然每次都返回新的,相同的字典(当然不同的实例,但它们是相同的)。当代码到达ObjectUtil.compare(),调试器打印以下错误:

Error #1034: Type Coercion failed: cannot convert "oranges" to QName. 

它为什么要比较一个QName的“橘子”键?

说明:如果我打电话给ObjectUtil.compare给出相同的实例作为参数,它会正常工作。我的意思是,如果我将getMyDictionary()的返回值保存到变量myDict中并调用ObjectUtil.compare(myDict, myDict),则错误不会出现,并且比较将通过。

任何人都可以点亮一下吗?我做错了吗?

回答

1

这是Flex sdk中的一个错误。 如果您不打算使用除String之外的任何其他密钥类型,请使用Object而不是Dictionary

private function getMyDictionary() : Object { 
     var myDictionary : Object = new Object(); 
     myDictionary["oranges"] = "orange"; 
     myDictionary["kiwis"] = "green"; 
     return myDictionary; 
} 
+0

感谢。有趣的事实:我发现这个类的一些版本[这里](http://svn.stefanix.net/filedetails.php?repname=actionscript&path=%2Fflex%2Fframeworks%2Fprojects%2Fframework%2Fsrc%2Fmx%2Futils%2FObjectUtil .as)并猜测:这个版本没有bug!此外,它是1426天的年纪。 – CamilB 2012-04-18 16:29:50

0

通过我发现这个文档的时候了,可这是有帮助的

mx.utils.ObjectUtil.compare(a:Object, b:Object, depth:int=-1):int

Compares the Objects and returns an integer value indicating if the first item is less than greater than or equal to the second item. This method will recursively compare properties on nested objects and will return as soon as a non-zero result is found. By default this method will recurse to the deepest level of any property. To change the depth for comparison specify a non-negative value for the depth parameter. Parameters: a Object. b Object. depth Indicates how many levels should be recursed when performing the comparison. Set this value to 0 for a shallow comparison of only the primitive representation of each property.

For example:

var a:Object = {name:"Bob", info:[1,2,3]}; 
    var b:Object = {name:"Alice", info:[5,6,7]}; 
    var c:int = ObjectUtil.compare(a, b, 0); 

In the above example the complex properties of a and b will be flattened by a call to toString() when doing the comparison. In this case the info property will be turned into a string when performing the comparison.

Returns:

Return 0 if a and b are null, NaN, or equal. Return 1 if a is null or greater than b. Return -1 if b is null or greater than a.

Language Version: 3.0

Player Version:Flash 9, AIR 1.1

Product Version:Flex 3