UILabel没有在ViewController swift3中获得更新

时间:2016-11-04 12:17:13

标签: ios uilabel swift3

我正在从json解析数据并将它们放入变量然后使用这些变量来改变UILabel的值,但是它们(UILabel)没有得到更新,我已经通过互联网搜索了他们已经说过它是结果从主线程以外的线程更新UIlabel但在这里我把它放在viewController但仍然没有得到更新 这是我的代码:

import UIKit

class NewsContentViewController: UIViewController {


    @IBOutlet weak var nuTitle: UILabel!
    var namesArray = String()
    var iD = String()
    var description1 = String()
    var iDArray = [String]()


    @IBOutlet weak var tctView: UITextView!


    override func viewDidLoad() {
        super.viewDidLoad()

        parseJSON()



        nuTitle.text = self.namesArray
        tctView.text = self.description1

        // Do any additional setup after loading the view.
    }




func parseJSON(){



    let requestURL: NSURL = NSURL(string: "XXXXXX" + iD)!
    print(requestURL)
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in


        let readableJSON = JSON(data: data! , options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)





            if let Name = readableJSON[0]["Title"].string as String! {
                var ID = readableJSON[0]["Id"].intValue
                var iDString = String (ID)
                self.namesArray.append(Name)

                var desc = readableJSON[0]["Description"].string as String!
                var type = readableJSON[0]["Type_Name"].string

            self.iDArray.append(iDString)

            self.description1 = desc!
            self.namesArray = Name


        } 
    }
    task.resume()


}


}

2 个答案:

答案 0 :(得分:1)

问题

这里的问题是parseJSON()内的任务将异步执行。 <到时候那个

nuTitle.text = self.namesArray
tctView.text = self.description1

将执行,任务尚未返回结果。

请详细了解iOS here

中的线程

解决方案

在这种情况下,一个正确的解决方案是添加completionBlock,这将返回您的数据。

override func viewDidLoad() {
    super.viewDidLoad()

    parseJSON { (description, name) in
        DispatchQueue.main.async {
            self.description1 = description
            self.namesArray = name
            nuTitle.text = self.namesArray
            tctView.text = self.description1
        }
    }
}

func parseJSON(completion: @escaping (_ description: String, _ name: String) -> Void) {
    let requestURL: NSURL = NSURL(string: "XXXXXX" + iD)!
    print(requestURL)
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in


        let readableJSON = JSON(data: data! , options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)

        if let Name = readableJSON[0]["Title"].string as String! {
            var ID = readableJSON[0]["Id"].intValue
            var iDString = String (ID)
            self.namesArray.append(Name)

            var desc = readableJSON[0]["Description"].string as String!
            var type = readableJSON[0]["Type_Name"].string

            self.iDArray.append(iDString)

            completion(desc!, Name)
        } 
    }
    task.resume()
}

答案 1 :(得分:0)

func parseJSON(){



    let requestURL: NSURL = NSURL(string: "XXXXXX" + iD)!
    print(requestURL)
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in


        let readableJSON = JSON(data: data! , options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)





            if let Name = readableJSON[0]["Title"].string as String! {
                var ID = readableJSON[0]["Id"].intValue
                var iDString = String (ID)
                self.namesArray.append(Name)

                var desc = readableJSON[0]["Description"].string as String!
                var type = readableJSON[0]["Type_Name"].string

            self.iDArray.append(iDString)

            self.description1 = desc!
            self.namesArray = Name
       // assign text here to label 
       nuTitle.text = self.namesArray
        tctView.text = self.description1


        }