核心数据返回0值

时间:2016-09-18 11:03:34

标签: ios swift core-data

这是我的代码,交货费应该是400或更多,但核心数据保存的交货费为0,当我打印出来后,当我打印出这里的交货费时打印(" TOTALAMT(转换)")它打印400

       let cartt = Cart(entity: entity!, insertIntoManagedObjectContext: context)


            var lt = String(ff.latt)
            var lg = String(ff.longg)
            var urlo = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(lt),\(lg)&destinations=\(self.customerLat!),\(self.customerLong!)&mode=driving&language=pl-PLe"

        print(urlo)

            var url = NSURL(string: urlo)
            let session = NSURLSession.sharedSession()
            session.dataTaskWithURL(url!) {
                (data, response, error) in
                let jsonObj = JSON(data: data!)
                let yy = jsonObj["rows"][0]["elements"][0]["distance"]["value"].int
                var converted:Int?
                print(yy)
                if yy <= 5000{
                    converted = (yy! / 1000) * 80

                }else{
                    converted = (yy! / 1000) * 65

                }

                if converted <= 400{
                    converted = 400
                }



                print("TOTALAMT\(converted)")

                cartt.delivery = Int(converted!)

                }.resume()

        if let imageUrl = ff.imageUrl as? String{
            cartt.imageUrl = imageUrl
        }
        //img is from the outlet

        //save
        context.insertObject(cartt)

        do{
            try context.save()
            print(cartt.longg)
            print(cartt.delivery)
            print("saved")

        }catch{
            print("could not save ")
        }

0 个答案:

没有答案