我已经成功打印了JSON数据,但是,例如,如何将相同的数据放到UITextView上呢?
@IBOutlet weak var dataView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "apple.com")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) { (response, data, error) in
if let data = data {
print(String(data: data, encoding: .utf8)!)
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}
}
}
我要提取的字符串不是实际数据,这只是一个例子。
答案 0 :(得分:1)
如@Don所述,您需要在主线程上更新UI对象。喜欢:
DispatchQueue.main.async {
self.dataView.text = String(data: data, encoding: .utf8)!
}