Max*_*Max 4 uiimageview ios parse-platform swift
我以前一直在从Parse后端检索图像,使用以下代码行在UIImageView中的应用程序中显示:
let userPicture = PFUser.currentUser()["picture"] as PFFile
userPicture.getDataInBackgroundWithBlock { (imageData:NSData, error:NSError) -> Void in
if (error == nil) {
self.dpImage.image = UIImage(data:imageData)
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
"AnyObject?不能转换为'PFFile'; 你的意思是用'as!' 迫使低垂?
"有用的"Apple修复技巧提示"as!" 改变所以我添加!,但后来我得到错误:
"AnyObject?不能转换为'PFFile'
使用'getDataInBackgroundWithBlock'部分,我也得到错误:
无法使用类型'((NSData,NSError) - > Void)的参数列表调用'getDataInBackgroundWithBlock'
有人可以解释如何从Parse正确检索照片并使用Swift 1.2在UIImageView中显示它吗?
kis*_*umi 10
PFUser.currentUser()返回可选的type(Self?).因此,您应该将返回值解包为按下标访问元素.
PFUser.currentUser()?["picture"]
Run Code Online (Sandbox Code Playgroud)
下标得到的值也是可选类型.因此,您应该使用可选绑定来转换值,因为类型转换可能会失败.
if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {
Run Code Online (Sandbox Code Playgroud)
并且getDataInBackgroundWithBlock()方法的结果块的参数都是可选类型(NSData?和NSError?).所以,你应该为这些参数指定的可选类型,而不是NSData和NSError.
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
Run Code Online (Sandbox Code Playgroud)
代码修改了以上所有问题如下:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4131 次 |
| 最近记录: |