[英]FMDB usage with swift - returning a boolean
I am currently building an app in swift that uses FMDB to interact with a database that is stored on the device.我目前正在 swift 中构建一个应用程序,该应用程序使用 FMDB 与存储在设备上的数据库进行交互。 I chose to use FMDB since I have experience using it with Objective C, and it says it supports swift.我选择使用 FMDB,因为我有使用 Objective C 的经验,它说它支持 swift。
I am having problems with returning booleans in swift when executing a statement.执行语句时,我在快速返回布尔值时遇到问题。 Here is an image of the update functions available to me when using FMDB in an Objective C class, notice how the overwhelming majority return bools这是我在 Objective C 类中使用 FMDB 时可用的更新函数的图像,注意绝大多数返回布尔值
And here are the funcs available in swift:以下是 swift 中可用的函数:
Doesn't give me much to work with!没有给我太多的工作!
Here is an existing query I am currently using in an app (with names changed).这是我目前在应用程序中使用的现有查询(名称已更改)。
sql = @"INSERT INTO Table (IdValue, AnotherIDValue) VALUES (?, ?);";
BOOL success = [db executeUpdate:sql,
[NSNumber numberWithLong:preference.idvalue1],
[NSNumber numberWithLong:preference.idvalue2],
nil];
Once this statement runs I close off the database & return the boolean.一旦此语句运行,我将关闭数据库并返回布尔值。 This essentially gives me a completion block and lets me hold the UI up until the sql has successfully completed.这基本上给了我一个完成块,让我保持 UI 直到 sql 成功完成。
Unfortunately with swift I have alot less to work with, and I don't really understand the function inputs that return bools.不幸的是,使用 swift 时我的工作要少得多,而且我不太了解返回 bool 的函数输入。 So far in my swift database class I run updates like so:到目前为止,在我的 swift 数据库类中,我运行更新如下:
try! db.executeUpdate(sqlStatement, values: dataArray)
Giving me safety, if it fails, but no way to return a success boolean.给我安全,如果失败,但无法返回成功布尔值。 I am wondering if anyone has any suggestions for implementing the database class like I showed in objective c.我想知道是否有人对实现我在目标 c 中展示的数据库类有任何建议。
The only alternative I can see is rewriting the class in objective c, however I would prefer to keep this app 100% swift.我能看到的唯一替代方法是在目标 c 中重写类,但是我更愿意让这个应用程序 100% 快速。
From Adopting Cocoa Design Patterns in the "Using Swift with Cocoa and Objective-C" reference:来自“使用 Swift 与 Cocoa 和 Objective-C”参考中的采用 Cocoa 设计模式:
Swift automatically translates Objective-C methods that produce errors into methods that throw an error according to Swift's native error handling functionality. Swift 会根据 Swift 的原生错误处理功能自动将产生错误的 Objective-C 方法转换为抛出错误的方法。
Therefore the Objective-C method因此,Objective-C 方法
- (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
.进入 Swift,并且必须使用(的变体) 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?
如果您只对成功/失败状态感兴趣,而不对实际的错误消息(如您的目标代码)感兴趣,那么您可以使用try?
, which evaluates to nil
if the evaluation failed: ,如果评估失败,则评估为nil
:
let success = (try? db.executeUpdate(sqlStatement, values: dataArray)) != nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.