DanielWolfgangTrebek
DanielWolfgangTrebek

Reputation: 81

Trying to get my voting system to work and log in backend in Parse

I am trying to create a voting system and store it to my backend and have it come up for each individual picture and be stored for each picture. I created a column in my backend Parse called "count" but i cant seem to get the votes to be brought up or saved in the back and added. I am using a swipeGestureRecognizer to initiate the voting right for one up left for one down but i cant get the correct syntax at the switch statement for the -= and += and i get the error of Binary operator '+='/'-=' cannot be applied to operands of type '[(Int)]' and 'Int'what can i do to make the voting system work to both save in the backend and be brought up and shown each individual pics votes?

import UIKit
import Parse


class HomePage: UITableViewController {

    var images = [UIImage]()
    var titles = [String]()
    var imageFile = [PFFile]()
    var count = [Int]()


    override func viewDidLoad() {
        super.viewDidLoad()

        println(PFUser.currentUser())

        var query = PFQuery(className:"Post")


        query.orderByDescending("createdAt")

        query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil  {

                println("Successfully retrieved \(objects!.count) scores.")
                println(objects!)
                for object in objects! {

                        if let title = object["Title"] as? String {
                            self.titles.append(title)
                        }
                        if let imgFile = object["imageFile"] as? PFFile {
                            self.imageFile.append(imgFile)
                        }
                    if let voteCounter = object["count"] as? Int {
                        self.count.append(voteCounter)
                    }

                    self.tableView.reloadData()

                }
            } else {
                // Log details of the failure
                println(error)
            }
        }
    }





                /* println("Successfully retrieved \(objects!.count) scores.")

                for object in objects! {

                    self.titles.append(object["Title"] as! String)

                    self.imageFile.append(object["imageFile"] as! PFFile)

                    self.tableView.reloadData()

                }*/



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return titles.count

    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 500

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell

        myCell.rank.text = "21"
        myCell.votes.text = "\(count)"
        myCell.postDescription.text = titles[indexPath.row]

        imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

            if let downloadedImage = UIImage(data: data!) {

                myCell.postedImage.image = downloadedImage

            }
        }

        var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        myCell.postedImage.userInteractionEnabled = true;
        myCell.postedImage.addGestureRecognizer(swipeRight)


        var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Left
        myCell.postedImage.userInteractionEnabled = true;
        myCell.postedImage.addGestureRecognizer(swipeLeft)

        return myCell

    }


    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
                switch swipeGesture.direction {
                case UISwipeGestureRecognizerDirection.Right:
                      count += 1
                    println("Swiped right")
                case UISwipeGestureRecognizerDirection.Left:
                     count -= 1
                    println("Swiped Left")
                default:
                    break
                }
            }
        }

    }

this is what i have now but the voting still wont get logged into parse and the post.count += 1 and post.count-=1 is receiving error messages of 'PFObject' does not have a member named 'count' where am i going wrong?

parse

Upvotes: 0

Views: 190

Answers (1)

Vasil Garov
Vasil Garov

Reputation: 4931

First of all, what you display on screen should depend entirely on your Parse model. What I mean is - you increment a count property every time a user votes. What will happen if the user stays in this screen for an hour and by that time 10 more users also vote? This won't be updated in the current screen and the user will see votes that are not up to date.

So what you can do is to create an object which inherits from PFObject. This object will be tied to Parse and will be always up to date.

What you can start with is reading the documentation. This can help you too.

So the main idea is to have your Parse columns as properties of a PFObject subclass:

class Post: PFObject, PFSubclassing {
    @NSManaged var count: Int

    class func parseClassName() -> String! {
        return "Post"
    }
}

In your AppDelegate's application(_:didFinishLaunchingWithOptions:) method register your Parse class like this:

Post.registerSubclass()

When you want to change the count property you'll have to set it and then update the screen:

let post = PFObject(className: "Post")
//increment like this
post.count += 1
//decrement like this
post.count -= 1

//refresh screen
//call reloadData()

Upvotes: 2

Related Questions