swift从哪个方向开始阅读字典? [英] From which direction swift starts to read dictionaries?

查看:72
本文介绍了swift从哪个方向开始阅读字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问的问题,swift从哪个方向开始读取字典或数组

the question that I want to ask, from which directions swift starts to read dictionaries or arrays

当我输入这样的代码

let interestingNumber = [
    "Square": [1,4,9,16,25],
    "Prime": [2,3,5,7,11,13],
    "Fiboannci": [1,1,2,3,5,8],
    "asd":[2,3,4,5],
    "zxc":[3,4,5]
]

for (key,values) in interestingNumber{
    print(values)
}

输出为

[1, 4, 9, 16, 25]
[2, 3, 5, 7, 11, 13]
[1, 1, 2, 3, 5, 8]
[3, 4, 5]
[2, 3, 4, 5]

这不是确切的顺序,所以您知道为什么这样做会很快吗?有时也会变得与众不同!

this is not the exact order, so do you know why swift does this ? and it sometimes makes it different too!

我猜可能是按字符串顺序执行的,然后我尝试了一下,但我认为不是,所以为什么swift这样做呢?

I guessed may be it does it in string order, then I tried but I think it is not too, so why swift does do that ?

推荐答案

就像NSDictionary一样,Swift字典也不是按键或值排序的.订单将始终是未指定的. 如果需要对键进行排序,则唯一的选择是拥有一个有序键数组,并在该数组上使用for循环.

Just like for NSDictionary, Swift dictionaries are not ordered by key or value. The order will always be unspecified. If you need the keys to be sorted, your only option is to have an array of ordered keys, and use the for loop over this array.

摘自Apple文档( https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105 )

From apple documentation (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105)

字典

字典存储相同类型的键和之间的关联 未定义顺序 的集合中相同类型的值.每个 值与唯一键相关联,该唯一键用作以下内容的标识符 字典中的那个值.与数组中的项目不同, 字典没有指定的顺序.您在以下情况下使用字典 您需要基于其标识符来查找值,几乎相同 实际字典用于查找以下内容的定义的方式 一个特定的单词.

A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to look up values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.

这篇关于swift从哪个方向开始阅读字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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