如何在Swift中的字典中获取最后输入的值? [英] How to get last entered value in a Dictionary in Swift?

查看:139
本文介绍了如何在Swift中的字典中获取最后输入的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中的Dictionary中获取最后输入的值?例如,如何从下面获取值"CCC":

How do I get the last entered value in a Dictionary in Swift? For example how would I get the value "CCC" from below:

var dictionary = Dictionary<String, String>()
dictionary.updateValue("AAA", forKey: "111")
dictionary.updateValue("BBB", forKey: "222")
dictionary.updateValue("CCC", forKey: "333")

推荐答案

您所追求的通常被称为有序词典,即记住项目添加顺序的字典.快速的Google搜索显示了一个适合您的GitHub项目.

What you are after is generally known as a Ordered Dictionary, i.e. a dictionary that remembers the order that items have been added. A quick Google search turns up a GitHub project that may suit you.

https://github.com/lukaskubanek/OrderedDictionary

使用这作为您的字典的类型,您可以使用:

With this as the type of your dictionary, you could just use:

dictionary.last // returns Optional(("CCC", "333"))

GitHub页面包含有关如何将此类添加到项目中的详细信息.

The GitHub page has details on how you would go about adding this class to your project.

这篇关于如何在Swift中的字典中获取最后输入的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆