使用对象的属性将字典快速转换为数组(Swift transform dictionary into array using properties of object)

编程入门 行业动态 更新时间:2024-10-26 05:34:06
使用对象的属性将字典快速转换为数组(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]

更多推荐

本文发布于:2023-08-02 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字典   属性   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!