1.

There is a compile time error in the following code. Are you able to recognise it and explain why it occurs? What are some possible solutions?

Answer» STRUCT CAT{}FUNC showCat(cat: Cat?) { guard let c = cat else { print("No presence of cat") } print(c)}

The problem with the given code is that a guard's else block requires an escape path, which can be achieved by RETURNING, throwing an exception, or calling a @noreturn. Adding a return statement is the SIMPLEST approach:

func showCat(cat: Cat?) { guard let c = Cat else { print("No presence of cat") return } print(c)}

A fatalError() can also be called which is a @noreturn function:

struct Cat{}func showCat(cat: Cat?) { guard let c = Cat else { print("No presence of cat") fatalError() } print(c)}


Discussion

No Comment Found