Swift: Parsing JSON Dictionary Values

别等时光非礼了梦想. 提交于 2019-12-12 14:24:52

问题


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.


回答1:


@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 + " "
})


来源:https://stackoverflow.com/questions/36394305/swift-parsing-json-dictionary-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!