Greetings to all, dear! There is such code:
import UIKit class UchastokViewController: UIViewController { @IBOutlet weak var uchLabel: UILabel! @IBOutlet weak var uchText: UITextView! var baseURL:String = "http://mylandbroker.ru/api.php?action=getuchastok&nid=" var mainUch = uchastok() var nid:String = "" override func viewDidLoad() { super.viewDidLoad() getUchastok() uchLabel.text = mainUch.uchTitle uchText.text = mainUch.uchBody } func getUchastok() { let request = NSURLRequest(URL: NSURL(string: baseURL+nid)!) let urlSession = NSURLSession.sharedSession() let task = urlSession.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in if let error = error { print(error) return } if let data = data { self.parseJsonData(data) } }) task.resume() } func parseJsonData(data: NSData){ do { let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary let jsonLoans = jsonResult?["uch"] as! [AnyObject] for jsonLoan in jsonLoans { mainUch.uchNid = jsonLoan["nid"] as! String mainUch.uchTitle = jsonLoan["title"] as! String mainUch.uchBody = jsonLoan["body_value"] as! String //uchas.uchGeometry = jsonLoan["field_map4_geom"] as! String } } catch { print(error) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
In viewDidLoad()
I call the getUchastok()
function, which connects to the site, receives the response in JSON, parses it, and writes the result to the mainUch
variable. After that, I want to display the data in text fields, but I can not, because the variable mainUch
is empty. I receive the data correct, checked through the console. Tell me where I am and why I can not display the data in the text box and UILabel
?