With FMDB resultset's resultDictionary method returns a dictionary for each row where the keys are the column names which have a value or are NSNulls. So I am trying to figure out how best to handle these and where.
使用FMDB结果集的resultDictionary方法为每一行返回一个字典,其中键是具有值或NSNull的列名。所以我想弄清楚如何最好地处理这些和在哪里。
So with the new update of swift I need to change my code and in the process I am looking at some refactoring.
因此,随着swift的新更新,我需要更改我的代码,在这个过程中,我正在考虑一些重构。
So as it stands the following line of code's "as" needs to be made to "as?" or "as!" :
因此,以下代码行的“as”需要“as?”或“as!” :
resultsArray.append(resultSet.resultDictionary() as Dictionary<String,AnyObject>)
Robert stated I should handle them like this. But it got me thinking about a better solution and thus I am looking for some advice on how I should refactor:
罗伯特说我应该像这样处理它们。但它让我想到了一个更好的解决方案,因此我正在寻找一些关于如何重构的建议:
- I could get the optional, find nulls and replace the NSNulls with empty string literals "" and this handle empty strings somewhere else in my app.
- I could just leave them as is and force unwrap.
我可以获得可选的,找到空值并用空字符串文字“”替换NSNulls,并在我的应用程序中的其他地方处理空字符串。
我可以按原样离开它们并强行打开包装。
public class func queryGetAllRowsFromTableAsArrayOfHashes() -> Array<Dictionary<String,AnyObject>>?
{
public class func queryGetAllRowsFromTableAsArrayOfHashes() - > Array
var resultsArray = Array<Dictionary<String,AnyObject>>()
let database = DBUtility.getDatabaseHandle()
//Perform query and place result into a dictionary.
self.log().info("Getting all rows from: \(self.getTableName())")
if let resultSet = database.executeQuery(self.getAllRowsStatement()) {
self.log().info("Query run")
while resultSet.next() {
//adds a dictionary of the result row to the array.
resultsArray.append(resultSet.resultDictionary() as! Dictionary<String,AnyObject>)
}
self.log().info("Results array: \(resultsArray)")
return resultsArray;
}else{
self.log().error("DB Error: \(database.lastErrorMessage())")
}
return nil;
}
Anyone got some advice on where this should be tackled?
任何人都有一些建议应该解决这个问题?
1 个解决方案
#1
You can simply put a check in if statement like this
您可以简单地检查if语句
if var dict = resultSet.resultDictionary() as Dictionary<String,AnyObject>
{
resultsArray.append(resultSet.resultDictionary() as! Dictionary<String,AnyObject>)
}
It will only append the value in array if there is non null value. hope it will help you.If value is null then if statement will not be executed.
如果存在非null值,它只会将值附加到数组中。希望它会帮助你。如果value为null,那么if语句将不会被执行。
#1
You can simply put a check in if statement like this
您可以简单地检查if语句
if var dict = resultSet.resultDictionary() as Dictionary<String,AnyObject>
{
resultsArray.append(resultSet.resultDictionary() as! Dictionary<String,AnyObject>)
}
It will only append the value in array if there is non null value. hope it will help you.If value is null then if statement will not be executed.
如果存在非null值,它只会将值附加到数组中。希望它会帮助你。如果value为null,那么if语句将不会被执行。