mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2026-01-04 10:09:56 +03:00
Sync classref with 4.5 branch
This commit is contained in:
@@ -129,7 +129,7 @@ To add a key to an existing dictionary, access it like an existing key and assig
|
||||
|
||||
|
||||
|
||||
Finally, dictionaries can contain different types of keys and values in the same dictionary:
|
||||
Finally, untyped dictionaries can contain different types of keys and values in the same dictionary:
|
||||
|
||||
|
||||
.. tabs::
|
||||
@@ -180,6 +180,45 @@ The keys of a dictionary can be iterated with the ``for`` keyword:
|
||||
|
||||
|
||||
|
||||
To enforce a certain type for keys and values, you can create a *typed dictionary*. Typed dictionaries can only contain keys and values of the given types, or that inherit from the given classes:
|
||||
|
||||
|
||||
.. tabs::
|
||||
|
||||
.. code-tab:: gdscript
|
||||
|
||||
# Creates a typed dictionary with String keys and int values.
|
||||
# Attempting to use any other type for keys or values will result in an error.
|
||||
var typed_dict: Dictionary[String, int] = {
|
||||
"some_key": 1,
|
||||
"some_other_key": 2,
|
||||
}
|
||||
|
||||
# Creates a typed dictionary with String keys and values of any type.
|
||||
# Attempting to use any other type for keys will result in an error.
|
||||
var typed_dict_key_only: Dictionary[String, Variant] = {
|
||||
"some_key": 12.34,
|
||||
"some_other_key": "string",
|
||||
}
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
// Creates a typed dictionary with String keys and int values.
|
||||
// Attempting to use any other type for keys or values will result in an error.
|
||||
var typedDict = new Godot.Collections.Dictionary<String, int> {
|
||||
{"some_key", 1},
|
||||
{"some_other_key", 2},
|
||||
};
|
||||
|
||||
// Creates a typed dictionary with String keys and values of any type.
|
||||
// Attempting to use any other type for keys will result in an error.
|
||||
var typedDictKeyOnly = new Godot.Collections.Dictionary<String, Variant> {
|
||||
{"some_key", 12.34},
|
||||
{"some_other_key", "string"},
|
||||
};
|
||||
|
||||
|
||||
|
||||
\ **Note:** Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use :ref:`duplicate()<class_Dictionary_method_duplicate>`.
|
||||
|
||||
\ **Note:** Erasing elements while iterating over dictionaries is **not** supported and will result in unpredictable behavior.
|
||||
|
||||
Reference in New Issue
Block a user