2016-12-02 47 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变成浮动和整个数据库显示错误的值 –