Last active
August 29, 2015 14:23
-
-
Save jessesquires/e43298a91157d19b90d7 to your computer and use it in GitHub Desktop.
Avoid using var with Swift try/catch?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var results = [MyModel]() // must use var :( | |
do { | |
results = try fetch(request: request, inContext: context) | |
} | |
catch { | |
print("Fetch error: \(error)") | |
} | |
// use results | |
print("Results = \(results)") |
I would probably use an optional here, or bail out of this scope entirely.
let results2: [MyModel]?
do {
results2 = try fetch(request: request, inContext: stack.managedObjectContext)
} catch {
print("Error = \(error)")
// Less awkward, and still have to handle absence.
results2 = nil
}
print("Results = \(results2)")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision:
This is slightly better.