2016-12-02 50 views
1

我有一個字典,有不同的數據類型值從​​該字典我創建數據庫的插入查詢現在我的問題是如果我傳遞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"; 
} 
+3

如果你想過一個快樂的生活作爲一個Swift編碼器,現在停止使用NSDictionary,並使用Swift詞典。它們是打字和安全的。一個人不應該像'.value(forKey:(key as!String))那樣做?雙'在斯威夫特。只需在正確類型的Swift數組或字典上使用下標。 – Moritz

+0

可能相關:[是否有正確的方法來確定NSNumber是從使用Swift的Bool派生的?](http://stackoverflow.com/questions/30215680/is-there-a-correct-way-to-determine - 即-AN-的NSNumber-IS-派生從-A-布爾-全光照)。 –

+0

@EricAya我試着用你的建議,但結果相同 –

回答

0

我避免Any類型的字典時,我可以,但如果你的鑰匙是String,並使用共同的價值類型,你的代碼可以大大如果按照埃裏克綾的建議簡化。這裏是相當於insertRecord函數。

func insertRecord(_ dictionary: [String: Any], tableName: String) -> Bool { 
    let columns = dictionary.keys.joined(separator: ",") 
    let values = dictionary.values.map { "\($0)" }.joined(separator: ",") 
    return self.executeQuery("INSERT INTO \(tableName)(\(columns))values(\(values))") 
} 

enter image description here

也許你會被強制使用,因爲舊代碼的約束目標C類型。然而,儘可能地接受Swift類型和編程習慣用法。

+0

謝謝你的回答,但在代碼中,我們可以做一個像你在數據庫中做的插入查詢我把位置的double值總是存儲int,如果我寫前面的雙重條件,那麼我的country_id變成浮動和整個數據庫顯示錯誤的值 –