我试图在PFFile尝试从背景中提取图像之前检查它是否有数据。我试图这样做,因为如果你试图打开其中一个对象,而没有图像,我就会不断地受到致命的撞击!我的问题是我无法让数据检查开始工作。PFFile != nil不能工作,您也不能检查它是否与if (recipeImageData)一起存在,因为PFFile不符合布尔协议。任何帮助都将不胜感激!
下面是变量的声明:
var recipeImageData: PFFile = PFFile()下面是获取数据的函数:
override func viewWillAppear(animated: Bool) {
navItem.title = recipeObject["Name"] as? String
recipeImageData = recipeObject["Image"] as PFFile //Fatally crashes on this line
// Fetch the image from the background
if (recipeImageData) {
recipeImageData.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
self.recipeImage.image = UIImage(data: imageData)?
} else {
println("Error: \(error.description)")
}
})
}
}编辑:
我只是试了一下,因为我可能是在错误的区域进行检查。这是我更新的代码。
override func viewWillAppear(animated: Bool) {
navItem.title = recipeObject["Name"] as? String
if let recipeImageData = recipeObject["Image"] as? PFFile {
// Fetch the image in the background
recipeImageData.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
self.recipeImage.image = UIImage(data: imageData)?
} else {
println("Error: \(error.description)")
}
})
}
}发布于 2015-03-24 14:42:32
这个检查实际上工作得很好,还有另一个问题导致了崩溃。正确的代码张贴如下:
override func viewWillAppear(animated: Bool) {
navItem.title = recipeObject["Name"] as? String
if let recipeImageData = recipeObject["Image"] as? PFFile {
// Fetch the image in the background
recipeImageData.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
self.recipeImage.image = UIImage(data: imageData)?
} else {
println("Error: \(error.description)")
}
})
}
}https://stackoverflow.com/questions/29234518
复制相似问题