I do not understand how the line if let count = occurrences[character]
because count is an Int, and occurrences[character] is a Character
how the fuction compares a single character of the string to each other??
Declare a function occurrencesOfCharacters that calculates which characters occur in a string, as well as how often each of these characters occur. Return the result as a dictionary. This is the function signature:
func occurrencesOfCharacters(in text: String) → [Character: Int]
Hint: String is a collection of characters that you can iterate over with a for statement.
func occurrencesOfCharacters(in text: String) → [Character: Int] {
var occurrences: [Character: Int] = [:]
for character in text {
if let count = occurrences[character] {//How comprare a string with a number
occurrences[character] = count + 1
} else {
occurrences[character] = 1
}
}
return occurrences
}