Flex的字典字面 [英] Flex dictionary literal

查看:137
本文介绍了Flex的字典字面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flex的工作,我需要填充词典具有相当复杂的结构。基于本文档页面我试图创建一个字典文字通过语法如下:

  VAR defaultMarkingA =
        {
            类型:页面,
            displayText:XYZ,
            innerMarkings:
                {编号:'ABC',键入:页面,displayText:'ABC'}
            ]
        };
 

我希望这是什么code创建一个嵌套的字典结构有三个映射:

  1. 在类型 - >页
  2. 在displayText - >XYZ
  3. 在innerMarkings=>单元素数组里面包含另一个意思:
    1. 在身份证 - >ABC
    2. 在类型 - >页
    3. 在displayText - >ABC

什么是code实际创建为根据的getQualifiedClassName的对象。我在做什么错在这里?动作是无法处理一个嵌套的字典文字?

解决方案

你没有做错什么,这只是不是让你需要的结构方式。使用东西= {} 的缩写东西=新的对象()。有没有这样的简写来创建一个词典

您链接文档:

  

这是关联数组是Object类的一个实例,这意味着   每个键对应的属性名称。

  

ActionScript 3.0中引入了关联数组的一个先进典型   称为字典。字典,它们是实例   Dictionary类flash.utils包,使用键,可以是   任何数据类型,但通常是Object类的实例。其他   也就是说,字典的键不局限于String类型的值。

要得到你期望的结果,你就必须做到以下几点:

  VAR defaultMarkingA:字典=新词典();
defaultMarkingA [型] =页面;
defaultMarkingA [displayText] =XYZ;
defaultMarkingA [innerMarkings] =新的Array();

VAR字典:字典=新词典();
快译通[ID] =ABC;
快译通[型] =页面;
快译通[displayText] =ABC;

defaultMarkingA [innerMarkings]推(字典)。
 

没有什么错用了对象作为关联数组。我能看到的唯一的问题是,性能可能会使用对象之间的差异词典但你需要做一些分析,看是否有)有什么不同和b)如果差的问题。

Working in Flex I need to populate Dictionaries with a fairly involved structure. Based on this documentation page I tried creating a dictionary literal via the following syntax:

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

What I expect this code to create is a nested Dictionary structure with three mappings:

  1. "type" -> "page"
  2. "displayText" -> "XYZ"
  3. "innerMarkings" => single-element array with another Dictionary inside containing:

    1. "id" -> "ABC"
    2. "type" -> "page"
    3. "displayText" -> "ABC"

What the code actually creates is an "Object" according to getQualifiedClassName. What am I doing wrong here? Is actionscript unable to deal with a nested Dictionary literal?

解决方案

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.

这篇关于Flex的字典字面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆