i'm removing temporary file nsfilemanager this:
let fm = nsfilemanager() fm.removeitematurl(fileurl) // error: "call can throw not marked 'try'" i don't care if file there when call made , if it's removed, it.
what elegant way tell swift ignore thrown errors, without using do/catch?
if don't care success or not can call
let fm = nsfilemanager.defaultmanager() _ = try? fm.removeitematurl(fileurl) from "error handling" in swift documentation:
you use
try?handle error converting optional value. if error thrown while evaluatingtry?expression, value of expressionnil.
the removeitematurl() returns "nothing" (aka void), therefore return value of try? expression optional<void>. assigning return value _ avoids "result of 'try?' unused" warning.
if interested in outcome of call not in particular error thrown can test return value of try? against nil:
if (try? fm.removeitematurl(fileurl)) == nil { print("failed") } update: of swift 3 (xcode 8), don't need dummy assignment, @ least not in particular case:
let fileurl = url(fileurlwithpath: "/path/to/file") let fm = filemanager.default try? fm.removeitem(at: fileurl) compiles without warnings.
Comments
Post a Comment