gpt4 book ai didi

ios - 界面标签!没有正确更新

转载 作者:行者123 更新时间:2023-11-28 10:22:49 25 4
gpt4 key购买 nike

我的 self.message.text = (newContentArray[0] as!String) 不允许我打印到我的标签。我的标签连接正确,但没有加载我的数据。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var city: UITextField!
@IBOutlet weak var message: UILabel!

@IBAction func buttonPressed(sender: AnyObject) {

self.view.endEditing(true)

var urlString = "http://www.weather-forecast.com/locations/" + city.text.stringByReplacingOccurrencesOfString(" ", withString: "") + "/forecasts/latest"

var url = NSURL(string: urlString)

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

var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
var contentArray = urlContent!.componentsSeparatedByString("<span class=\"phrase\">")
var newContentArray = contentArray[1].componentsSeparatedByString("</span>")

self.message.text = (newContentArray[0] as! String)

}
task.resume()
}

最佳答案

您应该在主线程上更新您的用户界面。在响应 block 中添加:

dispatch_async(dispatch_get_main_queue(),{ 
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
var contentArray = urlContent!.componentsSeparatedByString("<span class=\"phrase\">")
var newContentArray = contentArray[1].componentsSeparatedByString("</span>")
self.message.text = (newContentArray[0] as! String)
})

关于ios - 界面标签!没有正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com