您的位置:首页 > 移动开发 > Swift

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

2020-07-26 11:21 1536 查看

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 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:))

In Swift 5, Apple introduced a “grouping by” dictionary initializer. According to the documentation, the initializer has a definition of:

在Swift 5中,Apple引入了“分组依据”字典初始化程序。 根据文档 ,初始化程序具有以下定义:

Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.

创建一个新字典,其字典的键是给定闭包返回的分组,其值是返回每个键的元素的数组。

To better understand the definition, let’s revisit the example that we saw at the beginning of this article.

为了更好地理解该定义,让我们重新阅读本文开头看到的示例。

Let’s say you have a

Device
struct and an array of
Device
objects as shown below:

假设您有一个

Device
结构和一个
Device
对象数组,如下所示:

To group all

Device
objects by category, we can initialize a dictionary by giving its initializer the source array and a closure that returns the grouping key. Take a look at the code snippet below:

要按类别对所有

Device
对象进行分组,我们可以通过给字典的初始化程序提供源数组和返回分组键的闭包来初始化字典。 看一下下面的代码片段:

As you can see, we just need to pass in the

Device
array and return the category as the grouping key, the initializer will take care of the grouping for us.

如您所见,我们只需要传入

Device
数组并返回类别作为分组键,初始化程序将为我们处理分组。

Dictionary.init(grouping:by:)
in action
Dictionary.init(grouping:by:)

We can even further simplify the above code snippet into a single line of code by using the shorthand argument names in Swift.

通过使用Swift中的简写参数名称 ,我们甚至可以将上述代码片段进一步简化为一行代码。

Pretty neat isn’t it?

很好,不是吗?

The above example demonstrates the most standard way of using the initializer. However, since the initializer allows us to define a closure that determines the grouping key, it is much more flexible than that.

上面的示例演示了使用初始化程序的最标准方法。 但是,由于初始化程序允许我们定义一个确定分组键的闭包,因此它要灵活得多。

By using the same

deviceArray
, let's say we would like to group all Apple products together, we can actually define a closure that checks for the device 's name and group all devices with a name that contains "Macbook" and "iPhone".

通过使用相同的

deviceArray
,比如说我们要将所有Apple产品分组在一起,我们实际上可以定义一个闭包来检查设备名称,并将所有设备分组为包含“ Macbook”和“ iPhone”的名称。

按自定义对象分组 (Group by Custom Object)

In this section, we will look at how we can use a custom object as the grouping key of the

init(grouping:by:)
initializer.

在本节中,我们将研究如何使用自定义对象作为

init(grouping:by:)
初始化程序的分组键。

For demo purposes, let’s update the previous example by defining a

Company
struct and add a
company
variable to the
Device
struct. We will try to group all the devices by company in just a moment.

为了演示,让我们来更新前面的例子定义了一个

Company
结构和补充
company
变量的
Device
结构。 稍后,我们将尝试按公司对所有设备进行分组。

Next, let’s examine the definition of the

Dictionary
struct to find out the requirements that need to be fulfilled in order to become a dictionary key.

接下来,让我们检查

Dictionary
结构的定义,以找出成为字典键所需要满足的要求。

public struct Dictionary<Key, Value> where Key : Hashable

As can be seen from the above definition, any object that conforms to the Hashable protocol can be used as a dictionary key.

从上面的定义可以看出,任何符合Hashable协议的对象都可以用作字典键。

Pro tip:

专家提示:

Check out this great article to learn more about the

Hashable
protocol.

查阅这篇出色的 文章, 以了解有关

Hashable
协议的 更多信息 。

Therefore, we can go ahead and conform the

Company
struct to the
Hashable
protocol and leverage the
init(grouping:by:)
initializer to group all the devices by company.

因此,我们可以继续并使

Company
结构符合
Hashable
协议,并利用
init(grouping:by:)
初始化程序按公司对所有设备进行分组。

That’s it, we have successfully group all the

Device
objects by company.

就是这样,我们已经成功地将所有

Device
对象按公司分组。

结语 (Wrapping Up)

The

init(grouping:by:)
initializer is extremely useful and very easy to use.

init(grouping:by:)
初始化程序非常有用并且非常易于使用。

I find it comes in especially handy when I want to group an array of data into a dictionary in order to show them on a table view or collection view with multiple sections.

当我要将数据数组分组到字典中以便在具有多个部分的表视图或集合视图中显示它们时,我发现它特别方便。

Next time when you want to create a table view with multiple sections, make sure to give this method a try.

下次要创建具有多个部分的表视图时,请确保尝试使用此方法。

I hope this article can give you a clear idea on how to use the

Dictionary.init(grouping:by:)
initializer correctly.

我希望本文能为您提供有关如何正确使用

Dictionary.init(grouping:by:)
初始化程序的清晰思路。

If you like this article, feel free to check out my other articles related to Swift.

如果您喜欢这篇文章,请随时查看我与Swift相关的其他文章

If you have any questions, feel free to leave it in the comment section below or you can reach out to me on Twitter.

如果您有任何疑问,请随时在下面的评论部分中保留,或者可以在Twitter上与我联系。

Thanks for reading. 🧑🏻‍💻

谢谢阅读。 ‍💻

翻译自: https://levelup.gitconnected.com/grouping-array-elements-with-dictionary-in-swift-db2be2f0332b

swift 字典 添加元素

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐