繁体   English   中英

Parse:Swift: getDataInBackground,但数据不存在

[英]Parse:Swift: getDataInBackground, but data doesn't exist

我有解析问题。 我试图为我的用户保存个人资料图片,效果很好。 然后我想在图像视图中显示个人资料图片,如果用户查看他自己的个人资料。 问题是,当用户之前没有设置个人资料图片时,它会让我的应用程序崩溃,因为当它尝试执行 getDataInBackgroundWithBlock 函数时没有数据可查找。 我该如何解决这个问题? 这是我的代码,应该在 profilePicImageView 中显示相应的个人资料图片。 我之前在没有 else 语句的情况下尝试过,同样的问题:/

let profileImage = currentUser?.objectForKey("profilePic") as? PFFile

            profileImage!.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in

                if (error == nil){
                    let image:UIImage = UIImage(data: imageData!)!
                    self.profilePicImageView.image = image
                }else {

                    self.profilePicImageView.image = nil

                }

            })

ParseUI 有一个名为 PFImageView 的类,它是 UIImageView 的子类。 您可以不使用 UIImageView,而是执行以下操作: 1. 在 Interface Builder 中将类设置为 PFImageView 2. 将该 PFImageView 的出口设置为 profilePicImageView 3. 将代码更改为:

self.profilePicImageView,image = <Any placeholder>
if let profileImage = currentUser?.objectForKey("profilePic") as? PFFile {
   self.profilePicImageView.file = profileImage
   self.profilePicImageView.loadInBackground()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM