A function which return Int?
and throws error.
enum MyError : Error {
case anError
}
func test(value:Int) throws -> Int? {
guard value > 2 else {
throw MyError.anError
}
return value
}
And I try
it with if let
like this.
if let val = try? test(value: 4) {
print(val) // Optional(4)
}
val
is optional inside if let brackets? why?
and this also gives optional value
if let val = try? test(value: 4) as Int? {
print(val) // Optional(4)
}