【问题标题】:Attempting to check if PFFile has data or not尝试检查 PFFile 是否有数据
【发布时间】:2015-05-27 21:00:50
【问题描述】:

我试图在 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)")
      }
    })
  }
}

【问题讨论】:

  • 你试过了吗? if(recipeObject["Image"]) 之前 recipeImageData = recipeObject["Image"]
  • 这会导致 Parse 发生致命崩溃,尽管说有一个无法识别的选择器被发送到 parse 中的类
  • 是的,我意识到@Klevison 和更新的代码显示了我尝试检查它的检查,但我认为我的更新代码可能仍然是错误的。 PFFile 不能是可选的,所以可能是错误的
  • @KlevisonMatias 我也厌倦了你建议的检查,但我遇到了同样的致命错误,Parse 无法识别发送给它的选择器
  • 请粘贴您的模型(PFObject)。

标签: ios parse-platform pffile


【解决方案1】:

这个检查实际上工作得很好,还有另一个问题导致了崩溃。正确的代码贴在下面:

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)")
      }
    })
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多