Swift - Dictionaries: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „<syntaxhighlight lang="swift"> var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99] highestScore(scores: studentsAndScores) func highestScore(scores…“) |
|||
| (Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
| + | == Links == | ||
| + | [[Swift]] | ||
| + | == Beispiele == | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
| − | + | import UIKit | |
| + | studentsAndScores["Max"] = 65 | ||
| + | print("Number of students: \(studentsAndScores.count)") | ||
| + | print("Keys: \(studentsAndScores.keys) - note that the order is different each time") | ||
| + | showScores(scores:studentsAndScores) | ||
| + | print("James Score is: \(studentsAndScores["James"]!)") | ||
highestScore(scores: studentsAndScores) | highestScore(scores: studentsAndScores) | ||
| + | |||
| + | func showScores(scores: [String: Int]){ | ||
| + | for score in scores { | ||
| + | print(" \(score.key) \(score.value)") | ||
| + | } | ||
| + | } | ||
func highestScore(scores: [String: Int]) { | func highestScore(scores: [String: Int]) { | ||
var highest = scores.values.max() | var highest = scores.values.max() | ||
| − | print(highest ?? 0) | + | print(highest ?? 0) |
} | } | ||
| + | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Aktuelle Version vom 29. Dezember 2022, 15:38 Uhr
Links[Bearbeiten]
Swift
Beispiele[Bearbeiten]
import UIKit
studentsAndScores["Max"] = 65
print("Number of students: \(studentsAndScores.count)")
print("Keys: \(studentsAndScores.keys) - note that the order is different each time")
showScores(scores:studentsAndScores)
print("James Score is: \(studentsAndScores["James"]!)")
highestScore(scores: studentsAndScores)
func showScores(scores: [String: Int]){
for score in scores {
print(" \(score.key) \(score.value)")
}
}
func highestScore(scores: [String: Int]) {
var highest = scores.values.max()
print(highest ?? 0)
}