Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 字典(键、值)顺序_Ios_Swift_Dictionary - Fatal编程技术网

Ios 字典(键、值)顺序

Ios 字典(键、值)顺序,ios,swift,dictionary,Ios,Swift,Dictionary,假设我有以下代码 var dictionary = ["cat": 2,"dog":4,"snake":8]; // mutable dictionary var keys = dictionary.keys var values = dictionary.values for e in keys { println(e) } for v in values { println(v) } dictionary.key和dictionary.value的顺序是否相同 例如,如果

假设我有以下代码

var dictionary = ["cat": 2,"dog":4,"snake":8]; // mutable dictionary
var keys  = dictionary.keys
var values = dictionary.values
for e in keys {
    println(e)
}
for v in values {
    println(v)
}
dictionary.key和dictionary.value的顺序是否相同

例如,如果dictionary.keys是“dog”、“snake”、“cat” dictionary.values是否始终为4,8,2?
我在操场上试过,结果总是表明他们的顺序是一样的

不,不能保证顺序是一样的。从文件:

斯威夫特的字典类型是无序的集合。顺序 在对对象进行迭代时检索键、值和键值对 未指定字典


不,不能保证顺序相同。从文件:

斯威夫特的字典类型是无序的集合。顺序 在对对象进行迭代时检索键、值和键值对 未指定字典


属性的定义前面有以下内容 评论:

/// A collection containing just the keys of `self`
///
/// Keys appear in the same order as they occur as the `.0` member
/// of key-value pairs in `self`.  Each key in the result has a
/// unique value.
var keys: LazyBidirectionalCollection<MapCollectionView<[Key : Value], Key>> { get }

/// A collection containing just the values of `self`
///
/// Values appear in the same order as they occur as the `.1` member
/// of key-value pairs in `self`.
var values: LazyBidirectionalCollection<MapCollectionView<[Key : Value], Value>> { get }
///仅包含'self'键的集合`
///
///键的出现顺序与“.0”成员的出现顺序相同
///“self”中键值对的数目。结果中的每个键都有一个
///独特的价值。
var键:LazyBidirectionalCollection{get}
///仅包含“self”值的集合`
///
///值的出现顺序与“.1”成员的出现顺序相同
///“self”中键值对的数目。
var值:LazyBidirectionalCollection{get}
我对

键/值的出现顺序与
.0
/
.1
成员的出现顺序相同
self
中的键值对

这是
dictionary.keys
dictionary.values
返回中的键和值吗 “匹配”顺序

因此,字典的键值对没有指定的顺序,但是 第一,第二。。。
dictionary.values的元素

是对应于第一个、第二个。。。
dictionary.keys的元素

和值属性的定义前面有以下内容 评论:

/// A collection containing just the keys of `self`
///
/// Keys appear in the same order as they occur as the `.0` member
/// of key-value pairs in `self`.  Each key in the result has a
/// unique value.
var keys: LazyBidirectionalCollection<MapCollectionView<[Key : Value], Key>> { get }

/// A collection containing just the values of `self`
///
/// Values appear in the same order as they occur as the `.1` member
/// of key-value pairs in `self`.
var values: LazyBidirectionalCollection<MapCollectionView<[Key : Value], Value>> { get }
///仅包含'self'键的集合`
///
///键的出现顺序与“.0”成员的出现顺序相同
///“self”中键值对的数目。结果中的每个键都有一个
///独特的价值。
var键:LazyBidirectionalCollection{get}
///仅包含“self”值的集合`
///
///值的出现顺序与“.1”成员的出现顺序相同
///“self”中键值对的数目。
var值:LazyBidirectionalCollection{get}
我对

键/值的出现顺序与
.0
/
.1
成员的出现顺序相同
self
中的键值对

这是
dictionary.keys
dictionary.values
返回中的键和值吗 “匹配”顺序

因此,字典的键值对没有指定的顺序,但是 第一,第二。。。
dictionary.values的元素

是对应于第一个、第二个。。。字典的元素。键

这是正确的。尽管经验表明,对于简单的测试,它们以相同的顺序出现,但这并不能保证。如果您需要特定的顺序,请为键使用单独的数组。这是正确的。尽管经验表明,对于简单的测试,它们以相同的顺序出现,但这并不能保证。如果需要特定的顺序,请为键使用单独的数组。