Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do with Swift's "try?" that cause "Result of try? is unused"? [duplicate]

I want to call some method which can throw something. At the same time I don't care about the exception that may be thrown, I just want to call method. However, if I try to do something like this:

try? managedObjectContext.save()

I get warning "Result of try? is unused". What should I do in this case? Silence warning? How?

Do something like this:

let error: NSError = try? managedObjectContext.save()

? Expression becomes twice as large and I get unused constant.

like image 219
Fyodor Volchyok Avatar asked Oct 14 '15 10:10

Fyodor Volchyok


1 Answers

As your requirement, "At the same time I don't care about the exception that may be thrown, I just want to call method", do this:

try! managedObjectContext.save()

But it will crash if an error is thrown. So, use below code snip for safe:

_ = try? managedObjectContext.save()
like image 69
t4nhpt Avatar answered Oct 03 '22 23:10

t4nhpt