Ios 快速-获得双倍??使用UIImage时出错
我在处理来自Parse的图像时遇到问题。我创建了一个函数,从一个解析类中获取所有图像文件,并将它们保存到一个字典中(键值为objectId)。知道为什么和/或如何修复吗?代码如下:Ios 快速-获得双倍??使用UIImage时出错,ios,swift,parse-platform,Ios,Swift,Parse Platform,我在处理来自Parse的图像时遇到问题。我创建了一个函数,从一个解析类中获取所有图像文件,并将它们保存到一个字典中(键值为objectId)。知道为什么和/或如何修复吗?代码如下: private func generateDictionaryOfImages(imageKeyToLookup: String, dictionaryKeyToReturn: String) { for object in objectList { var currentObjectId
private func generateDictionaryOfImages(imageKeyToLookup: String, dictionaryKeyToReturn: String) {
for object in objectList {
var currentObjectId = object.objectId!
var image = UIImage()
let imageFile = object[imageKeyToLookup] as! PFFile
imageFile.getDataInBackgroundWithBlock({ (data: NSData?, error: NSError?) -> Void in
if data != nil {
if let imageData = data {
image = UIImage(data: imageData)!
}
}
})
// This imageDictionary is copied to the downloadedClientImages variable separately
self.imageDictionary[currentObjectId] = image
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Profile") as! ProfileTableViewCell
let title: String = grandCentral.returnValueForKeyAtIndexPath(indexPath.row, key: "forename") as! String
let currentUserId: String = grandCentral.returnCurrentUserId()
// Error is generated here. '@lvalue UIImage??' is not convertible to 'UIImage'
let image: UIImage = downloadedClientImages[currentUserId]
cell.setCell(title, image: image)
return cell
}
您需要在从字典访问它之后打开它
if let image = downloadedClientImages[currentUserId] {
cell.setCell(title, image: image)
}
这并不奇怪。问题是什么?如何声明
self.imageDictionary
和downloadedClientImages
呢?@ndmeiri self.imageDictionary=[String:UIImage]()和var downloadedClientImages:[String:UIImage?]=[String:UIImage](),然后downloadedClientImages=grandCentral.returnDictionaryOfImages()谢谢。为什么图像是可选的?我没有明确指出UIImage在任何时候都是可选的?有点困惑为什么会出现这个问题…?如果找不到密钥,字典不知道返回什么,所以它返回可选的,并强制您检查它。