0

Flexでの作業では、辞書にかなり複雑な構造を設定する必要があります。このドキュメントページに基づいて、次の構文で辞書リテラルを作成してみました。

    var defaultMarkingA =
        {
            type: 'page',
            displayText: 'XYZ',
            innerMarkings: [
                {id: 'ABC', type: 'page', displayText: 'ABC'}
            ]
        };

このコードが作成することを期待しているのは、3つのマッピングを持つネストされた辞書構造です。

  1. 「タイプ」->「ページ」
  2. "displayText"-> "XYZ"
  3. "innerMarkings" =>以下を含む別のディクショナリを含む単一要素配列:
    1. 「id」->「ABC」
    2. 「タイプ」->「ページ」
    3. 「displayText」->「ABC」

コードが実際に作成するのは、getQualifiedClassNameによる「オブジェクト」です。私はここで何が間違っているのですか?actionscriptはネストされた辞書リテラルを処理できませんか?

4

1 に答える 1

3

You're not doing anything wrong that's just not the way to make the structure you want. Using something = {} is shorthand for something = new Object(). There is no such shorthand to create a Dictionary.

From the docs you linked:

An associative array is an instance of the Object class, which means that each key corresponds to a property name.

and

ActionScript 3.0 introduces an advanced type of associative array called a dictionary. Dictionaries, which are instances of the Dictionary class in the flash.utils package, use keys that can be of any data type but are usually instances of the Object class. In other words, dictionary keys are not limited to values of type String.

To get your expected result you'd have to do the following:

var defaultMarkingA:Dictionary = new Dictionary();
defaultMarkingA["type"] = "page";
defaultMarkingA["displayText"] = "XYZ";
defaultMarkingA["innerMarkings"] = new Array();

var dict:Dictionary = new Dictionary();
dict["id"] = "ABC";
dict["type"] = "page";
dict["displayText"] = "ABC";

defaultMarkingA["innerMarkings"].push(dict);

There is nothing wrong with using an Object as an associative array. The only problem I could see is that performance may differ between using Object and Dictionary but you'd need to do some profiling to see if a) there is any difference and b) if that difference matters.

于 2012-07-27T23:47:53.080 に答える