4 Replies - 554 Views - Last Post: 12 March 2017 - 10:32 PM

#1 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

How to Get JSON Data Out Of dataTask in Swift 3 ?

Posted 12 March 2017 - 09:13 AM

Hello

I need to get data latitude and longitude from mySql , I tried to write the code to get it but it show zero value
could you advise me ? , please

var latitudeDb: Double = Double()
var longitudeDb: Double = Double()

override func viewDidLoad() {
   super.viewDidLoad()
   let url = URL(string: "http://www.MyGPS/service.php")!
   let request = URLRequest(url: url)        
   let task = URLSession.shared.dataTask(with: request) { data, response, error in            
   if  let data = data {                
         guard let parsedDataArray = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]] else { return }           
         for parsedDict in parsedDataArray! {
               //This gives the value of latitude
              if let latitude = parsedDict["latitude"] {    
                   print("latitude=\(latitude)")    
                    self.latitudeDb = Double(latitude as! String) ?? 0.0                 
                 }
                 //And this is for "longitude"
               if let longitude = parsedDict["longitude"] {                        
                   print("longitude=\(longitude)")                     
                   self.longitudeDb = Double(longitude as! String) ?? 0.0
                  }                 
               }         
           }           
           
        }

   task.resume()

  print("latitude=\(latitudeDb),longitude=\(longitudeDb)")            
   
}



the result below

Quote

latitude=13.2222
logitude=101.2222
latitude=0,longitude=0< --- Why the value is zero

This post has been edited by little_prince41: 12 March 2017 - 09:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Get JSON Data Out Of dataTask in Swift 3 ?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: How to Get JSON Data Out Of dataTask in Swift 3 ?

Posted 12 March 2017 - 09:23 AM

Seems like you are getting the data so probably an issue with conversion, right?

Explain what you believe this line is doing:

15	                    self.latitudeDb = Double(latitude as! String) ?? 0.0   

Was This Post Helpful? 0
  • +
  • -

#3 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

Re: How to Get JSON Data Out Of dataTask in Swift 3 ?

Posted 12 March 2017 - 09:33 AM

View Postmodi123_1, on 12 March 2017 - 09:23 AM, said:

Seems like you are getting the data so probably an issue with conversion, right?

Explain what you believe this line is doing:

15	                    self.latitudeDb = Double(latitude as! String) ?? 0.0   



I want convert data value to double for google map
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: How to Get JSON Data Out Of dataTask in Swift 3 ?

Posted 12 March 2017 - 09:40 AM

Explain to me the logic in that line. What do you believe the line is doing?

Hint - I rarely use the as! over as?.. also why is it wrapped in a double? Shouldn't the format be someVar = anotherVar as? someVar'sType ?
Was This Post Helpful? 0
  • +
  • -

#5 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

Re: How to Get JSON Data Out Of dataTask in Swift 3 ?

Posted 12 March 2017 - 10:32 PM

View Postmodi123_1, on 12 March 2017 - 09:40 AM, said:

Explain to me the logic in that line. What do you believe the line is doing?

Hint - I rarely use the as! over as?.. also why is it wrapped in a double? Shouldn't the format be someVar = anotherVar as? someVar'sType ?

it mean look like this
let aLat: String? = "11.123456"
let bLat: String? = "11"
let cLat: String? = nil

let a = Double(aLat!) ?? 0.0 // 11.123456
let b = Double(bLat!) ?? 0.0 // 11
let c = Double(cLat!) ?? 0.0 // 0


var dLati = 0.0

if let lat = latitude {
    dLati = (lat as NSString).doubleValue
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1