问题描述
我正在使用这个简单的代码从网站中提取一些纯文本.
I am using this simple code to extract some plain text from a website.
@IBAction func askWeather(sender: AnyObject) {
let url = NSURL(string: "http://www.weather-forecast.com/locations/" + userField.text! + "/forecasts/latest")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) -> Void in
if let urlContent = data{
let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)
let wArray = webContent?.componentsSeparatedByString("Day Weather Forecast Summary:</b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">")
let wCont = wArray![1].componentsSeparatedByString("</span>")
self.weatherResult.text = wCont[0]
}
else{
print("Sorry could not get weather information")
}
}
task.resume()
}
@IBOutlet var weatherResult: UILabel!
@IBOutlet var userField: UITextField!
在我按下按钮以获取信息之后,几秒钟什么都没有发生(例如10-20),然后我得到了正确的结果,但是我在xcode中得到了此消息:
And after i press the button to fetch the information nothing happens for several seconds(like 10-20) and then i get the correct result however i get this message in xcode:
This application is modifying the autolayout engine from a background
thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
我尝试阅读一些有关此问题的文章,但他们使用异步等线程来运行其代码.我不太确定我的问题是什么.
I tried reading some posts on others having this problem but they were using threads like async etc. to run their code. Im not really sure what the problem is in my case.
谢谢!
推荐答案
我猜测self.weatherResult.text = wCont[0]
正在修改类似UILabel
或类似内容的内容,在这种情况下,您尝试更改用户界面的一部分来自后台线程–大禁忌.
I'm guessing that self.weatherResult.text = wCont[0]
is modifying something like a UILabel
or similar, in which case you're trying to change part of your user interface from a background thread – a big no-no.
尝试这样的代码:
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.weatherResult.text = wCont[0]
}
这篇关于此应用程序正在从后台线程swift2.0修改自动布局引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!