Returns a new dictionary containing only the key-value pairs that have non-nil
values as the result of transformation by the given closure.
SDK
- Xcode 10.2+
Framework
- Swift Standard Library
Declaration
Parameters
transform
A closure that transforms a value.
transform
accepts each value of the dictionary as its parameter and returns an optional transformed value of the same or of a different type.
Return Value
A dictionary containing the keys and non-nil
transformed values of this dictionary.
Discussion
Use this method to receive a dictionary with non-optional values when your transformation produces optional values.
In this example, note the difference in the result of using map
and compact
with a transformation that returns an optional Int
value.
Complexity: O(m + n), where n is the length of the original dictionary and m is the length of the resulting dictionary.