I have a swift method that I'm using to update Parse in the backend when the button is tapped. Votes are being updated: The method acts as a voting system, incrementing a label every time it's tapped. This action is happening in a Custom Cell:

    @IBOutlet weak var votesLabel: UILabel!

    var parseObject:PFObject?
   @IBAction func happyBtn(sender: AnyObject) {

    if(parseObject != nil) {

        if var votes: Int? = parseObject!.objectForKey("votes") as? Int {

            parseObject!.setObject(votes!, forKey: "votes")

          votesLabel?.text = "+\(votes!)"
            // print(votes)


Was wondering how I can optimize this function to send the data to and from Parse in the most efficient manner?

All help is appreciated!

  • 1
    \$\begingroup\$ Well done on the clean-up :-) \$\endgroup\$
    – Mast
    Nov 30, 2015 at 21:42
  • \$\begingroup\$ Thanks @Mast! Any clues as to how to improve this would be greatly appreciated as well :) \$\endgroup\$
    – Lukesivi
    Dec 1, 2015 at 7:08
  • \$\begingroup\$ Wouldn't recommend putting the update code inside the function that handles the tap event, not really reusable \$\endgroup\$ Dec 3, 2015 at 5:25
  • \$\begingroup\$ Furthermore, are you noticing performance issues or you just want to optimize for the sake of it? Could consider using saveEventually() \$\endgroup\$ Dec 3, 2015 at 5:26
  • \$\begingroup\$ Currently, this works fine. However, I'm assuming that the sending of data could be done in a more efficient manner than sending to the server every time the user votes. Do you know what I mean? Lastly, I've tried putting this code in the TableViewController in the tap event but it doesn't get executed. @DanielGalasko \$\endgroup\$
    – Lukesivi
    Dec 3, 2015 at 7:41

1 Answer 1


I have some feedback concerning the usage of Swift optionals in your code.

The main point is that there is far too much "forced unwrapping". The forced unwrap operator ! can and should be avoided in most cases. Instead of comparing with nil and forced unwrapping

if (parseObject != nil) {

use optional binding to test and get the unwrapped value:

if let theParseObject = parseObject {

Or, if parseObject is expected to be non-nil when the method is called (e.g. because it is set up in viewDidLoad), declare it as an implicitly unwrapped optional

var parseObject : PFObject!
// ...

There is no reason to declare var notes as an optional Int?. With

if var votes = parseObject.objectForKey("votes") as? Int { ... }

you get rid of more forced unwrap operators.

Finally, the votesLabel textfield outlet is expected to be non-nil (otherwise you did not connect it correctly in the interface builder). That is the reason why it is declared as an implicitly unwrapped optional UILabel!. There is no need for optional chaining in

 votesLabel?.text = "+\(votes!)"

Putting it all together, the method should look like this:

@IBAction func happyBtn(sender: AnyObject) {

    if let theParseObject = self.parseObject {
        if var votes = theParseObject.objectForKey("votes") as? Int {
            theParseObject.setObject(votes, forKey: "votes")
            votesLabel.text = "+\(votes)"

No ! anymore!

  • \$\begingroup\$ hey this works, thanks a lot! Why is forced unwrapping bad practice in this case? Why is it bad practice in general? Does this increase speed, execution? Overall question: what does this change do compiler wise? Thank you once again. \$\endgroup\$
    – Lukesivi
    Dec 14, 2015 at 19:33
  • 1
    \$\begingroup\$ @lukesIvi: Each x! is a possible candidate for a runtime crash. It relies on you doing the appropriate checks before. Doing "test and unwrap" once saves some instructions later and is safer. See also stackoverflow.com/questions/29717210/…. \$\endgroup\$
    – Martin R
    Dec 14, 2015 at 19:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.