swift 字典 添加元素_在Swift中用字典对数组元素进行分组

这篇博客介绍了如何在Swift中使用字典对数组元素进行分组,讲解了字典添加元素的方法,并提供了相关链接深入理解该话题。
摘要由CSDN通过智能技术生成

swift 字典 添加元素

This article is originally published at https://swiftsenpai.com on June 30, 2020.

本文最初于 2020年6月30日 发布在 https://swiftsenpai.com 上。

Imagine you have an array of Device objects and you want to group them by category as shown in the image below:

假设您有一个Device对象数组,并且想要按类别对它们进行分组,如下图所示:

Grouping Array Elements With Dictionary in Swift
Grouping Device object by category
按类别分组设备对象

How should you go about in solving this problem?

您应该如何解决这个问题?

Before Swift 5, the most straightforward way is to loop through each device in the array and manually assign each element to its respective category. In Swift 5, Apple has introduced a generic dictionary initializer to help developers deal with this kind of situation with just 1 single line of code.

在Swift 5之前,最直接的方法是遍历数组中的每个设备,并将每个元素手动分配给其各自的类别。 在Swift 5中,Apple引入了一个通用的字典初始化程序,以帮助开发人员仅用一行代码即可处理这种情况。

Wondering how this can be done? Read on to find out more.

想知道如何做到这一点? 请继续阅读以了解更多信息。

介绍init(grouping:by :) (Introducing init(grouping:by:)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值