iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)

iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误),第1张

概述我以前一直在从Parse后端检索图像,使用以下代码行在UI ImageView中的应用程序中显示: let userPicture = PFUser.currentUser()["picture"] as PFFileuserPicture.getDataInBackgroundWithBlock { (imageData:NSData, error:NSError) -> Void in 我以前一直在从Parse后端检索图像,使用以下代码行在UI ImageVIEw中的应用程序中显示:
let userPicture = PFUser.currentUser()["picture"] as PFfileuserPicture.getDataInBackgrounDWithBlock { (imageData:NSData,error:NSError) -> VoID in    if (error == nil) {            self.dpImage.image = UIImage(data:imageData)    }}

但我得到错误

‘AnyObject?’ is not convertible to ‘PFfile’; dID you mean to use ‘as!’
to force downcast?

“有用的”Apple修复技巧提示“as!”改变所以我添加!,但后来我得到错误:

‘AnyObject?’ is not convertible to ‘PFfile’

使用’getDataInBackgrounDWithBlock’部分,我也得到错误:

Cannot invoke ‘getDataInBackgrounDWithBlock’ with an argument List of type ‘((NSData,NSError) -> VoID)’

有人可以解释如何从Parse正确检索照片并使用Swift 1.2在UIImageVIEw中显示它吗?

解决方法 PFUser.currentUser()返回可选类型(Self?).因此,您应该将返回值解包为按下标访问元素.
PFUser.currentUser()?["picture"]

下标得到的值也是可选类型.因此,您应该使用可选绑定来转换值,因为类型转换可能会失败.

if let userPicture = PFUser.currentUser()?["picture"] as? PFfile {

getDataInBackgrounDWithBlock()方法的结果块的参数都是可选类型(NSData?和NSError?).所以你应该为参数指定可选类型,而不是NSData和NSError.

userPicture.getDataInBackgrounDWithBlock { (imageData: NSData?,error: NSError?) -> VoID in

修改上述所有问题的代码如下:

if let userPicture = PFUser.currentUser()?["picture"] as? PFfile {    userPicture.getDataInBackgrounDWithBlock { (imageData: NSData?,error: NSError?) -> VoID in        if (error == nil) {            self.dpImage.image = UIImage(data:imageData)        }    }}
总结

以上是内存溢出为你收集整理的iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)全部内容,希望文章能够帮你解决iOS – 从UIImageView中的Parse检索并显示图像(Swift 1.2错误)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存