Dictionary
private class
system.Dictionary
Description
A Dictionary is a collection of key-value pairs, which uses strict equality (===) for key comparison.
- When an object is used as a key, the object's identity is used to look up the object, and not the value returned from calling
toString()on it. - Keys are unique. Re-assigning to a key simply overwrites the existing value. Declaring a dictionary with duplicate keys does not error, but takes the last value.
- When typing a dictionary variable or instantiating a new Dictionary, the element types must be specified in
<KeyType, ValueType>format.
Dictionaries in LoomScript can be instantiated via their constructor function, or with a literal syntax using curly brackets ({}):
var d1:Dictionary.<String, Number> = new Dictionary.<String, Number>();
d1['one'] = 1;
d1['two'] = 2;
var d2:Dictionary.<String, Number> = { 'three': 3, 'four': 4 };
Dictionary values are accessed via the square bracket operators ([]) and a key:
var n:Number = d1['two'];
d2['five'] = 5;
Iteration over dictionaries can be done in a couple of ways:
- with a
for..inloop, for iteration by key - with a
for eachloop, for iteration by value
var d:Dictionary.<String, Number> = {
'one' : 1,
'two' : 2,
'three' : 3,
};
for (var key:String in d) {
trace('d["' +key +'"] =', d[key]);
}
for each(var val:Number in d) {
trace(val);
}
API overview
Constructor
Dictionary
(
weakKeys: Boolean
= false
)
Creates a new Dictionary object
Functions
Public functions
| clear (): Void |
native Removes all of the key-value pairs in the Dictionary |
| deleteKey ( key: Object ): Void |
native Removes a key-value pair from the dictionary given the key |
| fetch ( key: Object , defaultValue: Object , thisObject: Object = null ): Object |
Returns the value for the given key if found in the dictionary, or else the default value |
| intercept ( dict: Object , read: Function , write: Function ): Void |
static native Intercepts all lookups and writes and passes them to the provided functions |
| mapToObject ( dictionary: Dictionary.<String, Object> , object: Object ): Void |
static Assigns a Dictionary's values to the corresponding fields (if present) of a given Object |
Constructor
Dictionary ( weakKeys: Boolean = false )
Creates a new Dictionary object. @param weakKeys Set to true to indicate that the dictionary should not hold a reference to its keys. If the key is garbage collected, it will be removed from the Dictionary. (Please note that a full GC may need to be run for the key to be removed from the weak dictionary).
Attributes
Functions
deleteKey ( key: Object ): Void
native
Removes a key-value pair from the dictionary given the key.
Parameters
| key: Object | The key of the key-value pair to remove from the Dictionary. |
fetch ( key: Object , defaultValue: Object , thisObject: Object = null ): Object
Returns the value for the given key if found in the dictionary, or else the default value.
Parameters
| key: Object | The key for the value to retrieve from the Dictionary. |
| defaultValue: Object | The value to return if the key is not found in the Dictionary. This may be a function that will accept the key and generate a value to return; its signature should be: function(key:Object):Object |
| thisObject: Object = null | Required if the defaultValue generator is an instance method and not a local or static function. |
getFullTypeName
(): String
Inherited from Object
native
Gets the fully qualified type name of the Object. The fully qualified type name includes the package of the type.
Returns
| String | fully qualified type name of the Object. |
getType
(): Type
Inherited from Object
native
Gets the Type that describes the Object.
Returns
| Type | The Type that describes the object. |
getTypeName
(): String
Inherited from Object
native
Gets the type name of the Object.
Returns
| String | type name of the Object. |
intercept ( dict: Object , read: Function , write: Function ): Void
static native
Intercepts all lookups and writes and passes them to the provided functions.
The Dictionary is not modified after intercept() is called, so if you leave it empty you'll see all traffic.
Parameters
| dict: Object | The dictionary to intercept. |
| read: Function | A callback in the form of function(dict:Object, key:Object):Object that returns the value for dict[key]. |
| write: Function | A callback in the form of function(dict:Object, key:Object, value:Object):void that updates dict[key] with value. |
mapToObject ( dictionary: Dictionary.<String, Object> , object: Object ): Void
static
Assigns a Dictionary's values to the corresponding fields (if present) of a given Object.
Parameters
| dictionary: Dictionary.<String, Object> | A dictionary of String to Object pairs, to be applied to the given Object. |
| object: Object | The object to update with values from the given dictionary. Only keys provided by the dictionary that exist in the object will be modified. |