简体   繁体   中英

Swift: Parsing JSON Dictionary Values

I'm trying to parse JSON data. What I can do is to save JSON data as an NSData file. Then I can read it as NSDictionary and set some values to respective text fields. What I cannot do is to put those values from JSON directly to respective text fields. The source of the data is Open Weather Map. That's not really important for me. I just need data to practice JSON parsing. Anyway, the following is what I have.

@IBOutlet weak var coordField1: UITextField!
@IBOutlet weak var countryField1: UITextField!
@IBOutlet weak var ideeField1: UITextField!
@IBOutlet weak var nameField1: UITextField!
@IBOutlet weak var cntField1: UITextField!
@IBOutlet weak var codField1: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    enum JSONError: String, ErrorType {
        case NoData = "ERROR: no data"
        case ConversionFailed = "ERROR: conversion from JSON failed"
    }

    let urlPath = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=fb1b5dcb756e9a52c49ee6377a02d879"
    guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
    let request = NSMutableURLRequest(URL:endpoint)
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data,response,error) -> Void in
        do {
            guard let dat = data else { throw JSONError.NoData }
            guard let jsonDict = try NSJSONSerialization.JSONObjectWithData(dat,options:[]) as? NSDictionary else { throw JSONError.ConversionFailed }
            //print(json)

            if let items1 = jsonDict["city"] as? NSDictionary {
                if let a = items1["coord"] {
                    if let lat = a["lat"] as? NSNumber {
                        print("\(lat.stringValue)") // It prints "55.75222"
                        self.coordField1.text = "lat: " + lat.stringValue as String + " "
                    }
                    if let lon = a["lon"] as? NSNumber {
                        print("\(lon.stringValue)") // √
                        self.coordField1.text = self.coordField1.text! + "lon: " + lon.stringValue as String
                    }
                }
            }

            let jdata:NSData = NSKeyedArchiver.archivedDataWithRootObject(jsonDict)
            jdata.writeToFile(self.filePath1(), atomically:true) // saving JSON as an NSData file

        } catch let error as JSONError {
            print(error.rawValue)
        } catch {
            print(error)
        }
        }.resume()
    }       
}

Xcode has forced me to put self before each text field. I can image why. Anyway, as a result, I have empty text fields. The application doesn't crash. What am I doing wrong?

Thanks.

@J.Wang has pointed out the right thing. The problem is UI is not updating on the main thread. As docs states the the completionHandler will be executed on the delegate queue so we have to dispatch it with the main queue. By the way i also got warning that this application is trying to modify autolayout on the background thread...

dispatch_async(dispatch_get_main_queue(), { () -> Void in
   self.coordField1.text = "lat: " + lat.stringValue as String + " "
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM