Swift:for-in有两个值

时间:2021-04-10 18:09:43

I started learning C some weeks ago and today I started learning Swift. The code is the following:

几周前我开始学习C,今天我开始学习Swift。代码如下:

    import Foundation

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

Why do I need kind in the for-in thingy? For "Prime", "Square", ..., right? Can I work with that somehow, too?

为什么我需要在for-in thingy中使用?对于“Prime”,“Square”,......,对吗?我也能以某种方式使用它吗?

“Add another variable to keep track of which kind of number was the largest, as well as what that largest number was.”

“添加另一个变量来跟踪哪个数字最大,以及最大数字是多少。”

How do I build that in?

我该如何构建它?

2 个解决方案

#1


0  

import Foundation
var largest = 0;
var largestKind: String?;

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 8, 16, 25],
]


for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number;
            largestKind = kind;
        }
    }

}
println("The number \(largest) is from the type \(largestKind)");

That's my solution at the moment. However, the output is

这是我目前的解决方案。但是,输出是

The number 25 is from the type Optional("Square")

数字25来自可选类型(“Square”)

How do I get rid of the 'Optional("")? I just want the word Square. I tried removing the question mark (var largestKind: String?; to var largestKind: String;) but I get an error doing that.

如何摆脱'Optional(“”)?我只想要广场这个词。我尝试删除问号(var largestKind:String?; to var largestKind:String;)但是我得到了一个错误。

#2


0  

For those who have the same question, this is another solution I've found. var largestKind is still optional because of String? but the exclamation mark at the end \(largestKind!) makes it possible to access the value without having that optional stuff around the actual content.

对于那些有同样问题的人来说,这是我发现的另一种解决方案。由于String,var largestKind仍然是可选的?但最后的感叹号\(largestKind!)使得访问该值成为可能,而实际内容周围没有可选的东西。

import Foundation
var largest = 0;
var largestKind: String?;

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 8, 16, 25],
]


for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number;
            largestKind = kind;
        }
    }

}
println("The number \(largest) is from the type \(largestKind!).");

#1


0  

import Foundation
var largest = 0;
var largestKind: String?;

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 8, 16, 25],
]


for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number;
            largestKind = kind;
        }
    }

}
println("The number \(largest) is from the type \(largestKind)");

That's my solution at the moment. However, the output is

这是我目前的解决方案。但是,输出是

The number 25 is from the type Optional("Square")

数字25来自可选类型(“Square”)

How do I get rid of the 'Optional("")? I just want the word Square. I tried removing the question mark (var largestKind: String?; to var largestKind: String;) but I get an error doing that.

如何摆脱'Optional(“”)?我只想要广场这个词。我尝试删除问号(var largestKind:String?; to var largestKind:String;)但是我得到了一个错误。

#2


0  

For those who have the same question, this is another solution I've found. var largestKind is still optional because of String? but the exclamation mark at the end \(largestKind!) makes it possible to access the value without having that optional stuff around the actual content.

对于那些有同样问题的人来说,这是我发现的另一种解决方案。由于String,var largestKind仍然是可选的?但最后的感叹号\(largestKind!)使得访问该值成为可能,而实际内容周围没有可选的东西。

import Foundation
var largest = 0;
var largestKind: String?;

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 8, 16, 25],
]


for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number;
            largestKind = kind;
        }
    }

}
println("The number \(largest) is from the type \(largestKind!).");