我无法从服务器加载数据,因为我收到错误 - error =( Error?) nil none
。在错误发生之前,它会看到PFObject
的4个值。然后下载并节拍。并指向一行 - let detailPrognozS = object["detailPrognozS"] as! String
。我不确定是什么问题?
func detailObject() {
let query = PFQuery(className: "soccer")
query.findObjectsInBackground { (objects:[PFObject]?, error:Error?) in
if error == nil {
for object in objects! {
let detailPrognozS = object["detailPrognozS"] as! String
let detailTitleS = object["detailTitleS"] as! String
let detailTextS = object["detailTextS"] as! String
let imageDetail = object["detailImageS"] as! PFFile
DispatchQueue.main.async { [unowned self] in
self.prognozDetail.text = detailPrognozS
self.textView.text = detailTextS
self.titleDetail.text = detailTitleS
}
imageDetail.getDataInBackground(block: { (data:Data?, error:Error?) in
if error == nil {
DispatchQueue.main.async { [unowned self] in
let imageData = data
self.imageDetail.image = UIImage(data: imageData!)
}
}
})
}
}
}
}
答案 0 :(得分:0)
请你更具体一点。我想会发生的事情是,当您在后台获取数据时,它会触发下一个for循环运行并且没有给它机会返回数据。
你应该真的清理这段代码。不应该在网络调用中设置uilabels文本,它可能做的就是返回一个带有对象和错误状态或nil的完成处理程序。请看一些MVC模式,我认为这会有所帮助。短期修复可能是添加“let dispatchGroup = DispatchGroup()”并在进入和离开时设置。希望这可以帮助。
https://developer.apple.com/documentation/dispatch/dispatchgroup https://www.raywenderlich.com/160651/design-patterns-ios-using-swift-part-12
答案 1 :(得分:0)
let query = PFQuery(className: "soccer")
query.findObjectsInBackground { (objects: [PFObject]?, error) -> Void in
if error == nil {
for object in objects! {
self.prognozDetail.text = object["detailPrognozS"] as? String
self.textView.text = object["detailTextS"] as? String
self.titleDetail.text = object["detailTitleS"] as? String
let imageDetail = object["detailImageS"] as? PFFile
imageDetail?.getDataInBackground(block: { (data:Data?, error:Error?) in
if error == nil {
if let imageData = data {
self.imageDetail.image = UIImage(data: imageData)
}
}
})
}
}
}