Swift - Dictionaries: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „<syntaxhighlight lang="swift"> var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99] highestScore(scores: studentsAndScores) func highestScore(scores…“)
 
Zeile 1: Zeile 1:
 
<syntaxhighlight lang="swift">
 
<syntaxhighlight lang="swift">
var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99]
+
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>

Version vom 28. Dezember 2022, 22:10 Uhr

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)
}