I am currently building an app in swift that uses FMDB to interact with a database that is stored on the device. I chose to use FMDB since I have experience using it with Object
From Adopting Cocoa Design Patterns in the "Using Swift with Cocoa and Objective-C" reference:
Swift automatically translates Objective-C methods that produce errors into methods that throw an error according to Swift’s native error handling functionality.
Therefore the Objective-C method
- (BOOL)executeUpdate:(NSString *)sql values:(NSArray *_Nullable)values error:(NSError *_Nullable __autoreleasing *)error
is mapped as
func executeUpdate(sql: String, values: [Any]?) throws
into Swift, and must be called with (a variant of) try
.
If you are only interested in the success/failure status, but not
in the actual error message (as in your Objective code), then
you can use try?
, which evaluates to nil
if the evaluation failed:
let success = (try? db.executeUpdate(sqlStatement, values: dataArray)) != nil