简体   繁体   English

苹果Swift中的For Loop

[英]For Loop in Apple Swift

Apple's newly released language Swift has an example on the official documentation . 苹果公司最新发布的语言Swift在官方文档中有一个例子。 Example is like this; 例子是这样的;

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

This is pretty simple but as an extra exercise,it requires to add another variable in order to return what type is the largest number (ie Square is the case here) 这非常简单,但是作为一项额外的练习,它需要添加另一个变量以返回最大的数字类型(即,此处为Square)

However, I can't seem to figure out what is "(kind,numbers)" here represent and how should I make my for-loop to go through all Dictionary(interestingNumbers) keys and find which key has the largest number. 但是,我似乎无法弄清楚这里表示的“(种类,数字)”是什么,以及如何使我的for循环遍历所有Dictionary(interestingNumbers)键并找出最大的键。

Thank you all for your help in advance 谢谢大家的帮助

Swift allows you to loop over a dictionary with tuple-syntax (key, value) . Swift允许您使用元组语法 (key, value)遍历字典。 So in every iteration of the for-loop Swift cares about reassigning the specified tuple-variables ( kind and number in your case) to the actual dictionary-record. 因此,在for循环的每次迭代中,Swift都会关心将指定的元组变量(在您的情况下为kindnumber )重新分配给实际的字典记录。

To figure out which Key includes the highest number in your example you can extend your code as follows: 要确定示例中哪个键包含的数字最大,可以按如下所示扩展代码:

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
var largestKey = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
            largestKey = kind
        }
    }
}
largest     // =25
largestKey  // ="Square"

Or if you want to practice the tuple-syntax try that (with the same result): 或者,如果您想练习元组语法,请尝试一下(结果相同):

var largest = 0
var largestKey = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            (largestKey, largest) = (kind, number)
        }
    }
}
largest     // =25
largestKey  // ="Square"

I can't seem to figure out what is "(kind,numbers)" here represents 我似乎无法弄清楚什么是“(种类,数字)”

It's a key-value pair (a tuple) containing the kind of the number. 这是包含数字种类的键/值对(元组)。 This syntax is called decomposition, basically, inside the loop you can access kind as the kind and numbers as the numbers that map for it. 这种语法称为分解,基本上,在循环内部,您可以访问kind作为种类,而numbers作为映射到它的数字。

For example, in some iteration: 例如,在某些迭代中:

kind  // "Prime"
numbers // [2, 3, 5, 7, 11, 13]

Quoting the guide: 引用指南:

You can also iterate over a dictionary to access its key-value pairs. 您还可以遍历字典以访问其键值对。 Each item in the dictionary is returned as a (key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple's members as explicitly named constants for use within in the body of the for-in loop. 迭代字典时,字典中的每个项目都将作为(键,值)元组返回,并且您可以将(键,值)元组的成员分解为明确命名的常量,以便在for-in循环的主体内使用。

for (kind, numbers) in interestingNumbers{}

This for loop actually enumerating the key/value pairs of dictionary interestingNumbers . 这for循环实际上枚举字典的键/值对interestingNumbers Where kind is the key and numbers is the correspoding value 其中kind关键 ,数字是相应的

kind:Prime       //Key
numbers: [2, 3, 5, 7, 11, 13] //Value  

Here the complete solution of the exercise 这里是练习的完整解决方案

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
var type: String = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
            type = kind
        }
    }
}
largest
type

However, I can't seem to figure out what is "(kind,numbers)" here represent 但是,我似乎无法弄清楚什么是“(种类,数字)”

The loop iterates over the dictionary, and every iteration gives you a key and associated value. 循环遍历字典,每次迭代都为您提供键和关联值。 Those are called kind (key) and numbers (value) here. 这些在这里称为kind (键)和numbers (值)。 You can choose any name you want. 您可以选择任何名称。

and how should I make my for-loop to go through all Dictionary(interestingNumbers) keys and find which key has the largest number. 以及如何使我的for循环遍历所有Dictionary(interestingNumbers)键,并找出哪个键具有最大的数字。

You get each key in turn in the kind loop variable. 您可以依次在kind循环变量中获取每个键。

Once you find one that results in a new largest , you can assign that to a result variable, say largestKind . 一旦找到一个导致新的largest结果,就可以将其分配给一个结果变量,例如largestKind

At the end of the loop, largestKind will contain the key of the array with the largest number (that number being the largest you already have). 在循环结束时, largestKind将包含阵列的使用数量最多的关键(这个数字作为largest ,你已经有了)。

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

This will return pair of (String,Int) which we have in Our Dictionary similar to function return multiple value as below, 这将返回我们在字典中具有的(String,Int)对,类似于函数返回多个值,如下所示,

   func refreshWebPage() -> (status:String,code:Int){
    //refresh logic
                return ("Success",200)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM