I want to catch the reason of error in map
function and notify it by print
.
I tried to use double quotation for emphasizing the reason inside my custom error message like this;
enum MyError: Error{
case creationError(_ message: String)
}
func creation() -> [Float]?{
...
do{
let arr_str = line.split(separator: parser.delimiter, omittingEmptySubsequences: false)
let arr = try arr_str.map{
(str) -> Float in
guard let val = Float(str) else{
throw MyError.creationError("could not convert string to float: \"\(str)\"")
}
return val
}
return arr
}
catch{
print("\(error)")
return nil
}
}
expected output is could not convert string to float: ""
but console output was like this;
creationError("could not convert string to float: \"\"")
if I try;
throw MyError.creationError("could not convert string to float: "\(str)"")
but Xcode says;
Expected ',' separator
How can l escape double quotation inside my custom error message? Or How can I get error message only in catch
block?