简体   繁体   中英

cannot update without specific objectId error using Swift and Parse

im using a gesture recognizer to increment votes in my backend and then using that to update the page and each cell. I am trying to link the image with the objectId as to having a way to update each images votes. issues keep coming up with my updateVote function because i cant seem to retrieve the objectId associated with the image from the backend. heres my code for the update vote and the gesture Recognizer:

 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:
            updateVote(true, objectId: String())
            println("Swiped right")
            case UISwipeGestureRecognizerDirection.Left:
            updateVote(false, objectId: String())
            println("Swiped Left")
            default:
                break
            }
        }
    }

func updateVote(increment: Bool, objectId : String) {
    // Create a pointer to an object of class Posts with id 'objectId'
    var object = PFObject(withoutDataWithClassName: "Post", objectId: objectId)

    // Increment the current value of the quantity key by 1
    if increment == true {
        object.incrementKey("count", byAmount: 1)
    } else {
        object.incrementKey("count", byAmount: -1)
    }

    // Save
    object.saveInBackgroundWithBlock(nil)
}

how can i retrieve the objectId because whenever i run this i keep getting the error cannot update without specific objectId? I get the println("swiped right or left") but i cant get the vote to increment how can i make that happen?

        import UIKit
        import Parse


        class HomePage: UITableViewController {

            var images = [UIImage]()
            var titles = [String]()
            var imageFile = [PFFile]()
            var votingObjects: [PFObject] = []
            var objectIds = [""]

         override func viewDidLoad() {
            super.viewDidLoad()

            println(PFUser.currentUser())

            println(PFUser.currentUser())

            var query = PFQuery(className:"Post")
            query.orderByDescending("createdAt")
            query.limit = 15
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in
                if error == nil  {
                    println("Successfully retrieved \(objects!.count) scores.")
                    println(objects!)
                    for objectRow in objects! {
                        let object = objectRow as! PFObject


                        if let objectIds = object["objectId"] as? String {
                            self.objectIds.append(objectIds)

                        }





                        //objectIds.append(object.objectId as? String)
                        // Adding them to the array

                        if let title = object["Title"] as? String {
                            self.titles.append(title)
                        }
                        if let imgFile = object["imageFile"] as? PFFile {
                            self.imageFile.append(imgFile)
                        }
                        self.votingObjects.append(object)
                    }
                    dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData() // Updating the tableView on the main thread - important. Do some research on Grand Central Dispatch :)

                    })
                } else {
                    println(error)
                    // Error
                }
            }
    }
 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 = votingObjects[indexPath.row]["Post"] as? String
        myCell.postDescription.text = titles[indexPath.row]

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

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

                myCell.postedImage.image = downloadedImage

            }
        }

this is the rest of my code on that page how can i retrieve an objectId from the array?

import UIKit
import Parse
import Foundation

class cell: UITableViewCell {

    @IBOutlet weak var rank: UILabel!
    @IBOutlet weak var votes: UILabel!
    @IBOutlet weak var postedImage: UIImageView!
    @IBOutlet weak var creditLink: UIButton!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var postDescription: UITextView!

}

my cell class

You are passing an empty string as the objectID objectId: String() " so you are not selecting any row to update.

Your function should pass the objectID of the row so parse can update it

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM