使用对象的属性将字典快速转换为数组(Swift transform dictionary into array using properties of object)
我有一个包含数组的对象的字典。 例如,让我们坐下每个对象工厂有对象管理器factory.manager 。
字典:
["key1": factory1] ["key2": factory2] ["key3": factory3]我需要通过将字典转换为数组来获取所有管理器:
[factory1.manager, factory2.manager, factory3.manager]我怎样才能使用map来避免使用循环。
I have a dictionary with objects that contains array. For example let's sat each object Factory has object manager factory.manager.
Dictionary:
["key1": factory1] ["key2": factory2] ["key3": factory3]I need to get all managers by transforming dictionary to an array:
[factory1.manager, factory2.manager, factory3.manager]How can I do it using map for example by avoiding using for cycle.
最满意答案
这将创建这些值的数组:
Array(dictionary.values.map{$0.manager})假设字典实例的类型为[String:Factory]
This will create an Array of those values:
Array(dictionary.values.map{$0.manager})assuming the dictionary instance is of type [String:Factory]
更多推荐
发布评论