user1406716
user1406716

Reputation: 9705

EXEC_BAD_INSTRUCTION when trying to get image from Parse

I am getting the error below in the following line: "let imgFile = object["imageFile"] as PFFile"

Code:

func getRightLevelInfo() {
    var query = PFQuery(className: "userstatus")
    query.whereKey("username", equalTo: PFUser.currentUser().username)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            println("Found something")
            for object in objects {
                println(object.objectId)

                let lastSuccessfulLevel = object["lastSuccessfulLevel"] as Int
                let nextLevel = lastSuccessfulLevel + 1
                let score = object["score"] as Int

                println("lastSuccessfulLevel = " + String(lastSuccessfulLevel) + ", Score = " + String(score))

                var queryLevel = PFQuery(className: "puzzledata")
                queryLevel.whereKey("lastleveldone", equalTo: String(nextLevel))
                queryLevel.findObjectsInBackgroundWithBlock {
                    (levels: [AnyObject]!, error: NSError!) -> Void in
                    if error == nil {

                        for level in levels {
                            self.wordAnswer = level["wordAnswer"] as String
                            self.wordJumbled = level["wordJumbled"] as String

                            println("For this level, wordAnswer = " + self.wordAnswer + ", wordJumbled = " + self.wordJumbled)

                            let imgFile = object["imageFile"] as PFFile


                            imgFile.getDataInBackgroundWithBlock({
                                (imageData: NSData!, error: NSError!) -> Void in
                                if (error == nil) {
                                    let image = UIImage(data:imageData)?
                                    println("Got Image Successfully")

                                } else {
                                    println("ERROR in getting image")
                                }

                            })//getDataInBackgroundWithBlock - end

                        }

                    } else {
                        println("%@", error) //couldn't find level data
                    }
                }


            }
        }
        else {
            println("%@", error)
        }
    }
}

Runtime error I am getting:

enter image description here

Although the error is in this line "let imgFile = object["imageFile"] as PFFile", you can also see that the parse database class does have that field with the exact same name "imgFile"

enter image description here

Upvotes: 0

Views: 83

Answers (1)

Wain
Wain

Reputation: 119031

It looks like a typo where you should replace

let imgFile = object["imageFile"] as PFFile

with

let imgFile = level["imageFile"] as PFFile

Upvotes: 1

Related Questions