我试图从解析中检索用户 Profiles 图像 . 我有一个集合视图,我正在检索人们发布的所有图像 . 我想在单元格中显示每个用户 Profiles 图像 . 我使用下面的代码
let query = PFQuery(className: "Posts")
query.findObjectsInBackgroundWithBlock{(question:[AnyObject]?,error:NSError?) -> Void in
if error == nil
{
if let allQuestion = question as? [PFObject]
{
self.votes = allQuestion
self.collectionView.reloadData()
}
}
}
和
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newview", forIndexPath: indexPath) as! NewCollectionViewCell
let item = self.votes[indexPath.row]
var UserQuery:PFQuery = PFUser.query()!
if let user = PFUser.currentUser() {
UserQuery.whereKey("uploader", equalTo: user)
}
UserQuery.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]?, error:NSError?)->Void in
if error == nil{
let user:PFUser = (objects as NSArray!).lastObject as! PFUser
cell.userName!.text = user.username
let PhotoFile:PFFile = user["profilePicture"] as! PFFile
PhotoFile.getDataInBackgroundWithBlock(){
(ImageData:NSData?, error:NSError?)->Void in
if error == nil{
let Image:UIImage = UIImage(data: ImageData!)!
cell.profileImageView.image = Image
}}}}
但是我得到一个错误说致命错误:在 let user:PFUser = (objects as NSArray!).lastObject as! PFUser
解包一个Optional值时意外地发现了nil
问题是,我无法从我创建的指针中检索此特定用户的配置文件图像和名称 . 谢谢 .
1 回答
所以不要这么说 . 任何时候你使用这样的感叹号,如果失败,你会要求崩溃 . 这就是发生的事情 . 所以不要使用感叹号 . 使用问号,如下所示: