swift 字典 添加元素_在Swift中用字典对数组元素进行分组
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
Deviceobjects 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
Devicestruct and an array of
Deviceobjects as shown below:
假设您有一个
Device结构和一个
Device对象数组,如下所示:
To group all
Deviceobjects 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
Devicearray 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
Companystruct and add a
companyvariable to the
Devicestruct. 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
Dictionarystruct 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
Hashableprotocol.查阅这篇出色的 文章, 以了解有关
Hashable协议的 更多信息 。
Therefore, we can go ahead and conform the
Companystruct to the
Hashableprotocol 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
Deviceobjects 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 字典 添加元素
- python中list中的元素为字典,对字典中value相同的进行分组
- html5基础 fieldset legend 对表单中的元素进行分组并添加标题
- 数组的创建/查找数组里面的内容/添加数组中元素/使用指定的字符串把数组链接起来/判断数组内是否有指定的数组元素/四种遍历进行输出数组中的元素有哪些
- 把删除数组里面字典文件,然后进行倒叙,删除指定的数组元素ios考核第四题
- 让无序数组元素进行排序,排序完后将排序后元素对应的原先元素的位置输出
- 将一个字典内的内value转换为集合:返回一个数组,此数组中包含输入字典的键值对中的数组的所有元素(为NSArray添加category)
- web前端,javascript元素及属性进行获取,设置,添加,删除
- python字典添加元素
- python 给字典 添加元素
- 如何通过JS,在html网页上进行添加元素,包括div 以及下拉框等
- 定义一个有10个元素的double型数组,然后从数组的最后一个元素倒着输出到第0个元素。并将数组元素进行累加。(java:数组和for循环)
- zabbix对主机组中的key进行分组批量添加到screen 推荐
- 有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).
- 字典对比:字典式元素组成的列表进行对比,如同键,值相减
- 2有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).
- swift按钮如何添加弹窗事件进行修改昵称或者密码等内容
- OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法
- 有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).
- python 实战10 对列表元素进行分类后加标签存入字典
- 个有10个元素的整型一维数组,用户输入9个数据,调用函数,对数组元素进行从小到大排序后,在函数中输入一个数,插入到数组中正确的位置,并输出