FMDB usage with swift - returning a boolean

前端 未结 1 1149
渐次进展
渐次进展 2021-01-25 21:25

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

相关标签:
1条回答
  • 2021-01-25 22:30

    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
    
    0 讨论(0)
提交回复
热议问题