0

I have declared a global variable 'var recipes = String: PFFile'. On parse.com, I created a column called "recipe" of type File and i have uploaded .txt files that contain the recipes. I am trying to load the contents of the .txt files from parse to a UITextView. I have tried many things and this is what I have:

var recipes = [String: PFFile]()
var valueToPass: String!
var valuePassed: String!
var appetizer = [String]()

var images = [String: UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFQuery(className: "Appetizers")
    query.orderByAscending("recipe")
    query.findObjectsInBackgroundWithBlock { (objects, error) in

        guard error == nil, let objects = objects else {
            print(error)
            return
        }

        for object in objects {

            // ... Appetizer Name

            let appetizerName = object.objectForKey("appetizer") as! String
            self.name.text = self.valuePassed

            // ... Recipe

            var recipes = [String: String]()

            let recipeFile = object["recipe"] as! PFFile

            do {

                let recipeData = try recipeFile.getData()
                let recipe = String(data: recipeData , encoding:NSUTF8StringEncoding)!
                print(recipe)  // recipe is now a string you can store or display

                self.myTextView.text = recipe

            } catch {
                print("something went wrong")
            }


            // ... Image

            let imageFile = object["imageFiles"] as!PFFile
            imageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in

                if error == nil {

                    let data = imageData

                } else {

                    print(error)

                }

                if let data = imageData {

                    self.images[appetizerName] = UIImage(data: data)
                    self.imageView.image = self.images[self.valuePassed]

                }
            })

        }

    }

}

I am able to display the recipe with the above code but not the one i chose. It is printing all the recipes in the logs but in a random order (but the same order each time) and the last one is the one being printed in the UITextView.

I can't seem to get the one i am choosing.

4
  • Please paste your code rather than a screen shot. You don't show the context of this code - how did you get object? Commented Apr 24, 2016 at 14:05
  • 2
    Its because there are more then one recipe returned by parse and in a for loop you are setting the recipe to the same textfield so it will always be the last one which will be set at the end. Where are you selecting the recipe and how are you filtering it at parse using query? Commented Apr 24, 2016 at 14:14
  • The query is ordering by Ascending and 'valuePassed' is the one being selected. Commented Apr 24, 2016 at 14:16
  • Any suggestions on how i can fix this? Commented Apr 24, 2016 at 14:16

2 Answers 2

1

You create a dictionary recipes of type [string: PFFile]. Presumably this is to contain all the recipes returned by Parse. Then you never populate that dictionary.

Later down in your code you have this line:

let recipeFile = object["recipe"] as! PFFile

Is that the PFFile you want to add to your dictionary?

If so, then you could use code like this:

let someKey = //Whatever string key you want to use to store a recipe
recipes[someKey] = recipeFile

You say "I am trying to load the contents of the .txt files from parse to a UITextView. "

So you're trying to add the combined contents of all the text files into a single UITextView?

In that case you would need to change this line:

let recipe = String(data: recipeData , encoding:NSUTF8StringEncoding)!

Say you create a new var:

var allRecipesText = String()

Then you could add a line below the line that coverts a recipe to a String:

let recipe = String(data: recipeData , encoding:NSUTF8StringEncoding)!
//concatenate the new recipe string to the end of allRecipiesText
allRecipiesText += recipe

Do you really need to save the PFFile objects, or do you want to save the converted text? And do you really want your recipes in a dictionary, or would you rather have them in an array?

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your answer @Duncan C. I select a row from a tableviewcontroller named "Appetizers" and it leads me to this viewController. What i am trying to do is from the row i selected, i want to display the image, the name of the recipe and then the recipe itself (for that selected item) to display in the UITextView named 'myTextView.text'
Ok, then you should build an array that contains structs with a recipe name, an image, and the recipe text. Modify your code that calls Parse so that in the for loop you create such an array. Then when the user taps on a cell, use the row number to index into your array and pass that struct to this view controller, that would display the information.
Not sure how to work with a struct yet, but what i have done so far works with the image and text, just not pulling the right recipe for the text View. I am using 'valuePassed' as the selected row from the tableview and that seems to be working except in the case of the table view
Have you already retrieved the Appetizers in the previous view controller? If so then you can just pass the PFObject and use that to retrieve the text. If not then you should at least add a whereKey clause to your query so that you only get the recipe you need
Yes, i retrieved a list of appetizers in the previous tableviewcontroller, once i select a particular row, a view controller appears with the image and name of the recipe and below that in a UITextView should be the recipe of the appetizer i chose.
|
0

With this code, i was able to display the recipe i chose in the previous tableviewcontroller:

let recipeFile = object["recipe"] as! PFFile


            do {

                let recipeData = try recipeFile.getData()
                let recipe = String(data: recipeData , encoding:NSUTF8StringEncoding)!
                // recipe is now a string you can store or display

                self.recipes[appetizerName] = recipe
                self.myTextView.text = self.recipes[self.valuePassed]

            } catch {

                print("something went wrong")
            }

appetizerName: used as the appetizer's name valuePassed: chosen recipe in the tableview controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.