首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图检查PFFile是否有数据

试图检查PFFile是否有数据
EN

Stack Overflow用户
提问于 2015-03-24 13:55:45
回答 1查看 634关注 0票数 2

我试图在PFFile尝试从背景中提取图像之前检查它是否有数据。我试图这样做,因为如果你试图打开其中一个对象,而没有图像,我就会不断地受到致命的撞击!我的问题是我无法让数据检查开始工作。PFFile != nil不能工作,您也不能检查它是否与if (recipeImageData)一起存在,因为PFFile不符合布尔协议。任何帮助都将不胜感激!

下面是变量的声明:

代码语言:javascript
复制
var recipeImageData: PFFile = PFFile()

下面是获取数据的函数:

代码语言:javascript
复制
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)")
      }
    })
  }
}

编辑:

我只是试了一下,因为我可能是在错误的区域进行检查。这是我更新的代码。

代码语言:javascript
复制
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)")
      }
    })
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-24 14:42:32

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

代码语言:javascript
复制
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)")
      }
    })
  }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29234518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档