Swift - 存储的值顺序在Dictionary中完全改变 [英] Swift - Stored values order is completely changed in Dictionary

查看:1071
本文介绍了Swift - 存储的值顺序在Dictionary中完全改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试显示字典格式的数据。下面,有三次尝试。首次尝试,输出顺序完全改变。第二次尝试,输出顺序与输入相同。但是,在第三次尝试中,我将变量声明为NSDictionary。我收到的确切输出。为什么这会改变字典?请指导我。我搜索了Swift的字典标签。但是我找不到。

I tried to display datas which is in Dictionary format. Below, three attempts are there. First attempt, output order is completely changed. Second attempt, output order is same as input. But, in third attempt, I declared variable as NSDictionary. Exact output I received. Why this changes in Dictionary? Kindly guide me. I searched for Swift's Dictionary tag. But I couldn't found out.

//First Attempt
var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]
        println(dict)

//output:
[name2: Roy, name1: Loy]

//Second Attempt
var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]
        println(dict)

//output:
[name2: Loy, name1: Roy]
-----------------------------------------------------------

//Third Attempt With NSDictionary
var dict : NSDictionary = ["name1" : "Loy", "name2" : "Roy"]
            println(dict)

//output:
{
    name1 = Loy;
    name2 = Roy;
}

另一个查询:我用过游戏场进行验证。我的屏幕截图如下:

ANOTHER QUERY: I have used play ground to verify. My screen shot is below:

这里,在NSDictionary中,我首先给了name5,但是在右边name2显示,然后,在println中,它以升序显示。为什么会这样???

Here, In NSDictionary, I gave name5 as first, but in right side name2 is displaying, then, in println, it is displaying in ascending order. Why this is happening??

这里,在字典中,我给了name5作为第一个,但是在右边name2正在显示,然后,在println中,它正在显示它是如何在Dictionary行上拍摄的。为什么会发生这种情况?

Here, In Dictionary, I gave name5 as first, but in right side name2 is displaying, then, in println, it is displaying, how it is taken on the Dictionary line. Why this is happening??

推荐答案

这是因为字典的定义:


字典存储相同类型的键与未定义的集合中相同类型的值之间的关联订购。

A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.

没有订单,它们可能与它们的输入方式不同。
这与NSSet相当。

There is no order, they might come out differently than they were put in.
This is comparable to NSSet.

编辑:


字典收集键值对。 NSDictionary不是简单地维护有序或无序的对象集合,而是根据给定的密钥存储对象,然后可以将其用于检索。

Dictionaries Collect Key-Value Pairs. Rather than simply maintaining an ordered or unordered collection of objects, an NSDictionary stores objects against given keys, which can then be used for retrieval.

还有 没有订单,但是出于调试目的,打印时会进行排序。

There is also no order, however there is sorting on print for debugging purposes.

这篇关于Swift - 存储的值顺序在Dictionary中完全改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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