Ios 解析:Swift:getDataInBackground,但数据不';不存在

Ios 解析:Swift:getDataInBackground,但数据不';不存在,ios,swift,parse-platform,Ios,Swift,Parse Platform,我对解析有问题。我试图为我的用户保存个人资料图片,效果非常好。然后,如果用户查看自己的个人资料,我想在图像视图中显示个人资料图片。问题是,当用户以前没有设置配置文件pic时,它会让我的应用程序崩溃,因为当它尝试执行getDataInBackgroundWithBlock函数时,找不到任何数据。我怎样才能解决这个问题?下面是我的代码,它应该在profilePicImageView中显示相应的ProfilePic。我以前在没有else语句的情况下尝试过,同样的问题:/ let profileImag

我对解析有问题。我试图为我的用户保存个人资料图片,效果非常好。然后,如果用户查看自己的个人资料,我想在图像视图中显示个人资料图片。问题是,当用户以前没有设置配置文件pic时,它会让我的应用程序崩溃,因为当它尝试执行getDataInBackgroundWithBlock函数时,找不到任何数据。我怎样才能解决这个问题?下面是我的代码,它应该在profilePicImageView中显示相应的ProfilePic。我以前在没有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()
}
self.profilePicImageView,图像=
如果让profileImage=currentUser?.objectForKey(“profilePic”)作为?PFFile{
self.profilePicImageView.file=profileImage
self.profilePicImageView.loadInBackground()
}

只需检查“imageData”是否为零。您看到了吗!在
UIImage(数据:imageData!)中-他们说“这些东西不会是零,如果它们是零,那么应用程序应该崩溃”。你需要检查选项是否为零。你读过iBooks上的Swift书吗?Optionals在那里解释得非常清楚。对不起,我实际上对编程非常陌生,我知道Optionals,并在没有!,但是xCode强迫我制作它们。但多亏了您的回答,我尝试了profileImage?.getDataInBackground(…)而不是!现在它工作得很好!所以这不是因为你认为的图像数据的强制展开,而是因为轮廓图像。。。谢谢你的帮助:)ps:我读了一些swift的书,但我更喜欢边做边学