简体   繁体   English

NSURLSession标签不更改文本Swift XCode 6

[英]NSURLSession label not changing text swift xcode 6

I'm trying to use NSURLSession to get data from a website and display it in the label. 我正在尝试使用NSURLSession从网站获取数据并将其显示在标签中。 The data is displaying in my println and the background is changing color based on the data but does not print the data in the label. 数据正在显示在我的println ,背景正在根据数据更改颜色,但不将数据打印在标签中。 Thanks in advance for the help! 先谢谢您的帮助!

The following code is in my viewDidLoad() function: 以下代码在我的viewDidLoad()函数中:

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in

            var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)

            if urlContent!.containsString("<div class=\"currentStatus warning\">") {

            var contentArray = urlContent!.componentsSeparatedByString("warning\">")

            var newContentArray = contentArray[1].componentsSeparatedByString("</div>")

            var statusNS = newContentArray[0] as NSString

            var stat = newContentArray[0] as String

            dispatch_async(dispatch_get_main_queue(), {
                println(stat)

                self.statusLabel.text = stat

                if statusNS.containsString("closed") {
                    self.view.backgroundColor = UIColor.redColor()
                } else {
                    self.view.backgroundColor = UIColor.greenColor()
                }

            })

            } else {
                dispatch_async(dispatch_get_main_queue()) {
                    self.statusLabel.text = "Hmm...Can't seem to find out"
                }
            }

        }

        task.resume()

Make sure your statusLabel is correctly connected in Interface Builder. 确保在Interface Builder中正确连接了statusLabel If you log the value of the status label, you will likely find nil . 如果您记录状态标签的值,则可能会找到nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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