getDataInBackgroundWithBlock

getDataInBackgroundWithBlock

本文介绍了解析文件查询和Swift 1.2错误-getDataInBackgroundWithBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Parse查询图像,并且继续遇到fatal error: unexpectedly found nil while unwrapping optional value.我不确定这是否与Swift和Xcode的最新更新有关,所以也许是在Parse端,或者肯定是用户错误.

I'm trying to query images from Parse and I continue to run into fatal error: unexpectedly found nil while unwrapping optional value. I'm not sure if this has to do with the recent update to Swift and Xcode, so maybe it's on the Parse end or it could certainly be user error.

getDataInBackGroundWithBlock只能接受的是NSData?NSError?作为可选项.如果我尝试解开它们或使用其他类型,它将无法编译.

The only things getDataInBackGroundWithBlock will accept are NSData? and NSError? as optionals. If I try unwrapping them or using other types it won't compile.

PFQuery返回预期的数据.只有在getDataInBackGroundWithBlock调用中尝试从PFFile切换到NSData时,问题才会出现.

The PFQuery returns data as expected. It's only when trying to switch from PFFile to NSData in the getDataInBackGroundWithBlock call that the issue comes up.

该框架已连接并正在运行.我已经测试了很多.

The framework is hooked up and running. I've tested it quite a bit.

这里是调用堆栈:

我已经连续12个小时从事此工作.我到处都看过,尝试了很多不同的东西.我不认识任何使用Swift或Xcode的人,您的帮助会步步为营.

I've been working on this for over 12 hours straight now. I have looked all over the place and have tried many different things. I don't know anyone who works with Swift or Xcode, your help would go miles and miles.

这里有我尝试都没有用的其他线程,其中一个是我自己的:

Here are other threads I have tried to no avail, one is my own:

无法解决解析使用块在后台获取数据"时出现错误

解析和Swift 1.2问题

解析getDataInBackgroundWithBlock不能按顺序提取

从Parse sdk检索Swift图像-崩溃了

还解析文档:

https://www.parse.com/docs/ios_guide#files/iOS (今天的例子就是我的出发点)

https://www.parse.com/docs/ios_guide#files/iOS (their example was my starting point today)

var photoArray: Array<UIImage> = []


override func viewDidLoad() {
    super.viewDidLoad()

    callPFObjectQuery()

    let testObject = PFObject(className: "QuestionMaster") //testobject
    testObject["foo"] = "bar" //testobject
    testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        println("Object has been saved.")

    }
}

func callPFObjectQuery() {

    var finalObjects: [PFObject] = []
    var nsdataObjects: NSData?

    var query: PFQuery = PFQuery(className: "QuestionMaster")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        for object in objects! {

            let imageFiles = object["questionImage"] as! PFFile!

//error line     imageFiles.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

                    if error == nil {

                        dispatch_async(dispatch_get_main_queue()) {

                            let image = UIImage(data: imageData!)
                            self.photoArray.append(image!)
                            println(self.photoArray[0])

                        }

                    }

                }
            }

        }
    }

更新,2015年11月12日-Swift 1.2:我发布此消息已经有好几个月了,因为它拥有超过1k的浏览量并且没有投票,我觉得最好回到上面,并确保我选择的内容确实正确.我没有经过再次测试,但是对Parse SDK和Swift变得非常非常熟悉(6-8k行),我对我提供的答案感到满意. Uttam Sinha非常接近,但是在getDataInBackgroundWithBlock中的完成处理程序上无法展开而不是使用可选选项进行包装是行不通的.它们必须是可选的,然后在块中展开. Ingouackaz完全错过了for循环,但是他的getDataInBackgroundWithBlock通话是正确的.

Update, November 12, 2015 - Swift 1.2: It's been quite a few months since I've posted this and since it has over 1k views and an answer with no upvotes I felt it best to go back over it and make sure what I selected was indeed correct. I haven't gone through and tested it again but having become very, very familiar(6-8k lines) with the Parse SDK and Swift I feel comfortable with the answer I provided. Uttam Sinha was very close but unwrapping instead of optionals on completion handler in getDataInBackgroundWithBlock won't work. They must be optionals and then unwrapped in the block. Ingouackaz missed the for-loop entirely, but his getDataInBackgroundWithBlock call is correct.

我的第一次尝试充其量是马虎,我可能很沮丧.对于刚刚开始的您,在数据查询之前的查询将以PFObject数组完成.因此,您需要遍历该数组,在每个项目上调用getDataInBackgroundWithBlock.

My first attempt was sloppy at best, I was probably frustrated. For those of you that are just beginning, the query right before the data query will complete with an array of PFObject's. Therefore you need to loop through that array, calling getDataInBackgroundWithBlock on each item.

我可能会使用object.valueForKey("questionImage")代替object["questionImage"],只是为了使其更加明确.我还将键字符串"questionImage"作为常量存储在键文件中,因此无需担心拼写错误.

Instead of object["questionImage"] I would probably go with object.valueForKey("questionImage") just to make it more explicit. I would also store the key string "questionImage" as a constant in a file of keys so there would be no need to worry about typos.

我不确定PDF文件和Parse.自从出现这种情况以来,我一直没有尝试过.但是现在我很好奇,因为如果可能的话,那将再次使我的工作更轻松.再次尝试后,我会更新.

I'm not sure about PDF files and Parse. I haven't attempted since that situation. But now I am curious because that would once again make my job easier if possible. I will update once I try again.

推荐答案

这有效.但是,此方法不适用于矢量图像.似乎情节提要中的便捷功能尚无法通过代码访问.至少不是这样.

This works. However, this method will not work with vector images. It seems that convenient feature in storyboard is not accessible through code yet. At least not in this way.

您可以注意到问题是imageFiles.getDataInBackgroundWithBlock行中的简单白色间距.必须减去一个空格并添加括号.感谢@ user2770692指出这一点.

You can note that the problem was simple white spacing in the imageFiles.getDataInBackgroundWithBlock line. Had to subtract a space and add parentheses. Thanks to @user2770692 for pointing that out.

func queryImages() {

    var query: PFQuery = PFQuery(className: "QuestionMaster")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        for object in objects! {

            let imageFiles = object["questionImage"] as! PFFile

            println(imageFiles)

            imageFiles.getDataInBackgroundWithBlock({
                (imageData: NSData?, error: NSError?) -> Void in
                if (error == nil) {
                    let image = UIImage(data:imageData!)
                    println(image)
                }

            })

        }

    }
}

这篇关于解析文件查询和Swift 1.2错误-getDataInBackgroundWithBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:32