FMDB usage with swift - returning a boolean

别等时光非礼了梦想. 提交于 2019-12-02 09:03:07

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!