1

I have one dictionary which has different data type value from that dictionary i create insert query for database now my problem is if i pass double value then also it goes in Int condition if i write Int condition up and Int condition down. I also try with isKind(of:) but same thing happened.

Here is my code with as keyword

     //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)
        }

My code with is code

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)
    }

What i try to store

{
"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";
}
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • 3
    If you want to live a happier life as a Swift coder, stop using NSDictionary right now, and use Swift dictionaries. They are typed and safe. One shouldn't do things like `.value(forKey: (key as! String)) as? Double` in Swift. Just use subscript on properly typed Swift arrays or dictionaries. – Eric Aya Dec 02 '16 at 13:50
  • Possibly related: [Is there a correct way to determine that an NSNumber is derived from a Bool using Swift?](http://stackoverflow.com/questions/30215680/is-there-a-correct-way-to-determine-that-an-nsnumber-is-derived-from-a-bool-usin). – Martin R Dec 02 '16 at 13:54
  • @EricAya i try with your suggestion but same result – Chirag Shah Dec 02 '16 at 15:39
  • @chiragshah Ah, too bad. Well then I hope somebody will be able to help you. Anyway, as Price Ringo also says, "Maybe you are forced to use the Objective C types because of legacy code constraints. However embrace Swift types and programming idioms wherever you can." Good luck! – Eric Aya Dec 02 '16 at 15:48

1 Answers1

0

I avoid Any typed dictionaries whenever I can, but if your keys are String and you use common value types, your code can be greatly simplified if you follow Eric Aya's advice. Here is the equivalent insertRecord function.

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

Maybe you are forced to use the Objective C types because of legacy code constraints. However embrace Swift types and programming idioms wherever you can.

Price Ringo
  • 3,424
  • 1
  • 19
  • 33
  • thank you for answer but in code we can made a insert query like you made in database i put the double value for location which always store int and if i write double condition ahead then my country_id become float and whole database display wrong value – Chirag Shah Dec 02 '16 at 15:38