我有一個字典,有不同的數據類型值從該字典我創建數據庫的插入查詢現在我的問題是如果我傳遞double值,那麼它也進入int條件,如果我寫入條件up和Int條件下降。我也嘗試與isKind(of :)但同樣的事情發生。不能differenciat double和int從NSDictionary
這裏是我的代碼以爲關鍵字
//MARK: INSERT RECORDS
func insertRecord(_ dictRecord : NSDictionary, tableName : String)->Bool
{
//Example:- INSERT INTO subject(sub_color,sub_name,sub_priority)values(125.562000,'Maths','15')
let strStart = "INSERT INTO " + tableName
var column : String = "("
var values : String = "("
var i = 0
for key in dictRecord.allKeys
{
column = (column as String) + (key as! String) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// column = "\(column) \(key) \(condition)" as NSString
if let strValue = dictRecord.value(forKey: (key as! String)) as? String
{
values = (values as String) + "\"" + strValue + "\"" + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// values = "\(values)'\(strValue)' \(condition) " as NSString
}else if let doubleValue = dictRecord.value(forKey: (key as! String)) as? Double
{
values = (values as String) + String (format: "%f",doubleValue) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}else if let numValue = dictRecord.value(forKey: (key as! String)) as? Int
{
values = (values as String) + String (format: "%d",numValue) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strNumValue = String(format: "%d", numValue)
// values = "\(values) \(strNumValue) \(condition) " as NSString
}
i = i + 1
}
// column = (column as String) + ")"
// values = (values as String) + ")"
column = "\(column))"
values = "\(values))"
let strQuery = String(format:"%@%@values%@",strStart,column,values)
return self.executeQuery(strQuery)
}
我與代碼爲代碼
func insertRecord(_ dictRecord : NSDictionary, tableName : String)->Bool
{
//Example:- INSERT INTO subject(sub_color,sub_name,sub_priority)values(125.562000,'Maths','15')
let strStart = "INSERT INTO " + tableName
var column : String = "("
var values : String = "("
var i = 0
for key in dictRecord.allKeys
{
column = (column as String) + (key as! String) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// column = "\(column) \(key) \(condition)" as NSString
if let strValue = dictRecord.value(forKey: (key as! String)) as? String
{
values = (values as String) + "\"" + strValue + "\"" + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// values = "\(values)'\(strValue)' \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Int
{
let numValue = dictRecord.value(forKey: (key as! String)) as? Int
values = (values as String) + String (format: "%d",numValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strNumValue = String(format: "%d", numValue)
// values = "\(values) \(strNumValue) \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Double
{
let doubleValue = dictRecord.value(forKey: (key as! String)) as? Double
values = (values as String) + String (format: "%f",doubleValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Float
{
let floatValue = dictRecord.value(forKey: (key as! String)) as? Float
values = (values as String) + String (format: "%f",floatValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}
i = i + 1
}
// column = (column as String) + ")"
column = "\(column))"
values = "\(values))"
let strQuery = String(format:"%@%@values%@",strStart,column,values)
return self.executeQuery(strQuery)
}
我嘗試存儲
{
"country_Lat" = "112.00";
"country_Long" = 112122.000; // if always store in database like 112122
"country_Name" = London;
"country_Status" = 1;
"country_id" = 2;
createddate = "12:00";
updateddate = "1:00";
}
如果你想過一個快樂的生活作爲一個Swift編碼器,現在停止使用NSDictionary,並使用Swift詞典。它們是打字和安全的。一個人不應該像'.value(forKey:(key as!String))那樣做?雙'在斯威夫特。只需在正確類型的Swift數組或字典上使用下標。 – Moritz
可能相關:[是否有正確的方法來確定NSNumber是從使用Swift的Bool派生的?](http://stackoverflow.com/questions/30215680/is-there-a-correct-way-to-determine - 即-AN-的NSNumber-IS-派生從-A-布爾-全光照)。 –
@EricAya我試着用你的建議,但結果相同 –