通过斯威夫特词典迭代 [英] Iterating Through a Dictionary in Swift

查看:126
本文介绍了通过斯威夫特词典迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有点困惑,Xcode在Swift编程语言指南中给了我这个实验的答案: //使用for-in来遍历字典(实验)

let interestingNumbers = [
Prime:[2,3,5,7,11,13],
斐波那契:[1,1,2,3,5,8],
Square:[1,4,9,16,25]
]
var最大= 0
(种类,数字)in interestingNumbers {
数字中的数字{
如果number>最大的{
最大=数
}
}
}
最大

我明白,当字典正在被转换时,最大的数字被设置为变量最大。不过,我对于Xcode为什么说最大被设置为5次,或1次或3次,这取决于每个测试而感到困惑。



当查看代码时,我看到它应该在Prime中单独设置6次(2,3,5,7,11,13)。那么它应该跳过斐波纳契中的任何数字,因为它们都是最小的,目前从Prime设置为13。然后,应该设置为16,最后在Square中设置为25,共计8次。我缺少一些完全明显的东西?

解决方案

Swift(和其他语言)中的字典未订购。当您遍历字典时,没有任何保证人可以将该顺序与初始化顺序相匹配。在这个例子中,Swift处理Square键。你可以通过在循环中添加一个print语句来看到这一点。 25是Square的第五个元素,所以最大的5个元素将被设置在Square中的5个元素的5次,然后将保持在25。

  let interestingNumbers = [
Prime:[2,3,5,7,11,13],
斐波纳契:[1,1,2,3,5,8],
Square:[1,4,9,16,25]
]
var maximum = 0
(kind,numbers)in interestingNumbers {
println kind:\(kind))
数字中的数字{
如果number>最大的{
最大=数
}
}
}
最大

打印:


  kind:Square 
kind :Prime
种:Fibonacci



I am a little confused on the answer that Xcode is giving me to this experiment in the Swift Programming Language Guide:

// Use a for-in to iterate through a dictionary (experiment)

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25]
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
largest

I understand that as the dictionary is being transversed, the largest number is being set to the variable, largest. However, I am confused as to why Xcode is saying that largest is being set 5 times, or 1 time, or 3 times, depending on each test.

When looking through the code, I see that it should be set 6 times in "Prime" alone (2, 3, 5, 7, 11, 13). Then it should skip over any numbers in "Fibonacci" since those are all less than the largest, which is currently set to 13 from "Prime". Then, it should be set to 16, and finally 25 in "Square", yielding a total of 8 times.

Am I missing something entirely obvious?

解决方案

Dictionaries in Swift (and other languages) are not ordered. When you iterate through the dictionary, there's no guarentee that the order will match the initialization order. In this example, Swift processes the "Square" key before the others. You can see this by adding a print statement to the loop. 25 is the 5th element of Square so largest would be set 5 times for the 5 elements in Square and then would stay at 25.

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25]
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    println("kind: \(kind)")
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
largest

This prints:

kind: Square
kind: Prime
kind: Fibonacci

这篇关于通过斯威夫特词典迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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