试图检查PFFile是否有数据

Sam*_*amG 2 ios parse-platform pffile

我试图在尝试从后台拉出图像之前检查PFFile是否有数据.我正在尝试这个,因为如果你试图打开其中一个物体并且没有图像,我会继续致命的崩溃!我的问题是我无法使数据检查工作.因为PFFile不符合布尔协议PFFile != nil,if (recipeImageData)所以无法检查它是否存在.任何帮助,将不胜感激!

这是变量的声明:

var recipeImageData: PFFile = PFFile()
Run Code Online (Sandbox Code Playgroud)

这是获取数据的功能:

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)")
      }
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我只是试着看到我可能正在检查错误的区域.这是我更新的代码.

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)")
      }
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*amG 6

这个检查实际上运行得很好,还有另一个导致崩溃的问题.正确的代码发布在下面:

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)")
      }
    })
  }
}
Run Code Online (Sandbox Code Playgroud)