Reputation: 1417
I want to compare two dictionaries using ObjectUtil.compare(). When the dictionaries are identical (two different instances, but with identical contents), the comparison fails with a strange exception:
Error #1034: Type Coercion failed: cannot convert "some_key" to QName.
Here's some code to make it clearer.
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;
}
}
}
As you can see, the constructor calls ObjectUtil.compare
with two dictionaries. The getMyDictionary()
method obviously returns new, identical dictionaries every time (different instances, of course, but they're identical). When the code gets to ObjectUtil.compare()
, the debugger prints the following error:
Error #1034: Type Coercion failed: cannot convert "oranges" to QName.
Why would it want to compare the "oranges" key with a QName?
Note: if I call ObjectUtil.compare
giving the same instance as arguments, it works properly. I mean that if I save the return value of getMyDictionary()
into a variable myDict
and call ObjectUtil.compare(myDict, myDict)
, then the error won't appear, and the comparison will pass.
Can anyone shed some light? Am I doing it wrong?
Upvotes: 0
Views: 1277
Reputation: 66
when gone through the docs i found this, may be this is helpful
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
Upvotes: 0
Reputation: 7304
This is a bug in Flex sdk.
Use Object
instead of Dictionary
if you're not going to use any key types except String
.
private function getMyDictionary() : Object {
var myDictionary : Object = new Object();
myDictionary["oranges"] = "orange";
myDictionary["kiwis"] = "green";
return myDictionary;
}
Upvotes: 1