Complete the code to declare a function that can throw an error.
func readFile() [1] { }The throws keyword declares that the function can throw an error.
Complete the code to call a throwing function with error handling.
do {
try readFile()
} [1] {
print("Error occurred")
}The catch block handles errors thrown by the try call.
Fix the error in the code to correctly handle errors when calling a throwing function.
let result = [1] readFile()The try keyword is required before calling a function that can throw an error.
Fill both blanks to create a dictionary comprehension that filters keys with values greater than 10.
let filtered = dictionary.filter { $0.value [1] 10 }.map { ($0.key, $0.value) }.reduce(into: [:]) { $0[$1.[2]] = $1.1 }The filter uses > to select values greater than 10, and key extracts the dictionary key.
Fill all three blanks to create a throwing function that reads a file and returns its content length if successful.
func readFile() [1] -> Int {{ let content = try String(contentsOfFile: "file.txt") return content.[2] }} let length = try? readFile() [3] nil
The function declares throws, returns the count of the content string, and uses ?? to provide a default value if the call fails.