GDictionary
A built-in data structure that holds key-value pairs.
Dictionary.swift:35class GDictionary
Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array.
You can define a dictionary by placing a comma-separated list of key: value
pairs inside curly braces {}
.
Creating a dictionary:
You can access a dictionary’s value by referencing its corresponding key. In the above example, points_dict["White"]
will return 50
. You can also write points_dict.White
, which is equivalent. However, you’ll have to use the bracket syntax if the key you’re accessing the dictionary with isn’t a fixed string (such as a number or variable).
In the above code, points
will be assigned the value that is paired with the appropriate color selected in my_color
.
Dictionaries can contain more complex data:
To add a key to an existing dictionary, access it like an existing key and assign to it:
Finally, dictionaries can contain different types of keys and values in the same dictionary:
The keys of a dictionary can be iterated with the for
keyword:
Citizens in SwiftGodot
Conformances
protocol ContentVariantRepresentable
Some of Godot’s builtin classes use ContentType for storage. This needs to be public because it affects their initialization, but SwiftGodot users should never need to conform their types to
ContentVariantRepresentable
.protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Typealiases
Type members
init(
) Constructs an empty
GDictionary
.init(alreadyOwnedContent: ContentType
) init(content: ContentType
) init(from: GDictionary
) Returns the same dictionary as
from
. If you need a copy of the dictionary, useduplicate(deep:)
.static var godotType: Variant.GType
static let zero: ContentType
static func != (lhs: GDictionary, rhs: GDictionary
) -> Bool Returns
true
if the two dictionaries do not contain the same keys and values.static func == (lhs: GDictionary, rhs: GDictionary
) -> Bool Returns
true
if the two dictionaries contain the same keys and values. The order of the entries does not matter.
Instance members
var content: ContentType
var debugDescription: String
Renders the dictionary using the
Variant
’sdescription
method.var description: String
Renders the dictionary using the
Variant
’sdescription
method.subscript(StringName
) -> Variant? Convenience subscript that uses a StringName as the key to access the elements in the dictionary. Merely wraps this on a Variant.
subscript(Variant
) -> Variant? subscript(String
) -> Variant? Convenience subscript that uses a String as the key to access the elements in the dictionary. Merely wraps this on a Variant.
func clear(
) Clears the dictionary, removing all entries from it.
func duplicate(deep: Bool
) -> GDictionary Creates and returns a new copy of the dictionary. If
deep
istrue
, innerGDictionary
andGArray
keys and values are also copied, recursively.func erase(key: Variant
) -> Bool Removes the dictionary entry by key, if it exists. Returns
true
if the givenkey
existed in the dictionary, otherwisefalse
.func findKey(value: Variant
) -> Variant Finds and returns the first key whose associated value is equal to
value
, ornull
if it is not found.func get(key: Variant, default: Variant
) -> Variant Returns the corresponding value for the given
key
in the dictionary. If thekey
does not exist, returnsdefault
, ornull
if the parameter is omitted.func getOrAdd(key: Variant, default: Variant
) -> Variant Gets a value and ensures the key is set. If the
key
exists in the dictionary, this behaves likeget(key:`default`:)
. Otherwise, thedefault
value is inserted into the dictionary and returned.func has(key: Variant
) -> Bool Returns
true
if the dictionary contains an entry with the givenkey
.func hasAll(keys: GArray
) -> Bool Returns
true
if the dictionary contains all keys in the givenkeys
array.func hash(
) -> Int64 Returns a hashed 32-bit integer value representing the dictionary contents.
func isEmpty(
) -> Bool Returns
true
if the dictionary is empty (its size is0
). See alsosize
.func isReadOnly(
) -> Bool Returns
true
if the dictionary is read-only. SeemakeReadOnly
. Dictionaries are automatically read-only if declared withconst
keyword.func keys(
) -> GArray Returns the list of keys in the dictionary.
func makeReadOnly(
) Makes the dictionary read-only, i.e. disables modification of the dictionary’s contents. Does not apply to nested content, e.g. content of nested dictionaries.
func merge(dictionary: GDictionary, overwrite: Bool
) Adds entries from
dictionary
to this dictionary. By default, duplicate keys are not copied over, unlessoverwrite
istrue
.func merged(dictionary: GDictionary, overwrite: Bool
) -> GDictionary Returns a copy of this dictionary merged with the other
dictionary
. By default, duplicate keys are not copied over, unlessoverwrite
istrue
. See alsomerge(dictionary:overwrite:)
.func recursiveEqual(dictionary: GDictionary, recursionCount: Int64
) -> Bool Returns
true
if the two dictionaries contain the same keys and values, innerGDictionary
andGArray
keys and values are compared recursively.func size(
) -> Int64 Returns the number of entries in the dictionary. Empty dictionaries (
{ }
) always return0
. See alsoisEmpty
.func values(
) -> GArray Returns the list of values in this dictionary.
Type features
init?(Variant
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.