0

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

user1406716
  • 9,565
  • 22
  • 96
  • 151
  • Why would that code be inside a loop in a different query completion block? – Wain Apr 10 '15 at 17:20
  • because I want to first get *lastSuccessfulLevel* and based on that get the right image for the right level. That is why I have the nested completion block. Is there an alternative way to achieve this? – user1406716 Apr 10 '15 at 17:22
  • so the image is on the level ? – Wain Apr 10 '15 at 17:30
  • Yes. Image is on the Parse class/db table "puzzledata". Basically, the problem is ... get one number (*lastSuccessfulLevel*) from one Parse table, and based on that look up another info (i.e. *imgFile*) from another class. – user1406716 Apr 10 '15 at 17:32
  • aah i see the problem now, thank you so much. it should be *level["imageFile"]* instead of *object["imageFile"]*. I am not on my mac to try this now, but will do this evening and mark you as the answer. That must be it. Thank you again (this is what copy paste does to you) – user1406716 Apr 10 '15 at 17:38

1 Answers1

1

It looks like a typo where you should replace

let imgFile = object["imageFile"] as PFFile

with

let imgFile = level["imageFile"] as PFFile
Wain
  • 118,658
  • 15
  • 128
  • 151