迅速使用FMDB-返回布尔值 [英] FMDB usage with swift - returning a boolean
问题描述
我目前正在快速构建一个使用FMDB与存储在设备上的数据库进行交互的应用程序.我选择使用FMDB,因为我有在Objective C上使用它的经验,并且它说它支持swift.
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 Objective C, and it says it supports swift.
我在执行语句时迅速返回布尔值时遇到问题.这是在Objective C类中使用FMDB时可以使用的更新功能的图像,请注意绝大多数用户是如何返回布尔值的
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
以下是迅速提供的功能:
And here are the funcs available in swift:
没有给我太多帮助!
这是我当前在应用中使用的现有查询(名称已更改).
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];
该语句运行后,我将关闭数据库&返回布尔值.从本质上讲,这为我提供了一个完成块,并使我可以保持UI直到sql成功完成.
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.
不幸的是,我的工作量大大减少了,而且我并不真正理解返回布尔值的函数输入.到目前为止,在我的快速数据库类中,我像这样运行更新:
Unfortunately with swift I have alot less to work with, and I don't really understand the function inputs that return bools. So far in my swift database class I run updates like so:
try! db.executeUpdate(sqlStatement, values: dataArray)
为我提供安全性(如果失败),但是没有办法返回成功布尔值.我想知道是否有人像我在目标c中所示对实现数据库类有任何建议.
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中重写该类,但是我希望此应用程序保持100%快速运行.
The only alternative I can see is rewriting the class in objective c, however I would prefer to keep this app 100% swift.
推荐答案
来自在 将Swift与Cocoa和Objective-C一起使用"参考:
From Adopting Cocoa Design Patterns in the "Using Swift with Cocoa and Objective-C" reference:
Swift根据Swift的本机错误处理功能自动将产生错误的Objective-C方法转换为引发错误的方法.
Swift automatically translates Objective-C methods that produce errors into methods that throw an error according to Swift’s native error handling functionality.
因此,Objective-C方法
Therefore the Objective-C method
- (BOOL)executeUpdate:(NSString *)sql values:(NSArray *_Nullable)values error:(NSError *_Nullable __autoreleasing *)error
映射为
func executeUpdate(sql: String, values: [Any]?) throws
转换为Swift,并且必须使用try
的变体来调用.
into Swift, and must be called with (a variant of) try
.
如果您仅对成功/失败状态感兴趣,而对
在实际的错误消息中(如您的目标代码),然后
您可以使用try?
,如果评估失败,则评估为nil
:
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
这篇关于迅速使用FMDB-返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!