Search code examples
iosswiftparse-platformpfquery

Swift: app crashes after deleting a column in Parse


In Parse I accidentally deleted a column called "likes" that counts the number of a likes a user receives for their blog post. I created the column again with the same name but now when I run my app it crashes and I receive this message "unexpectedly found nil while unwrapping an Optional value". It points to my code where its suppose to receive the "likes" in my cellForRowAtIndexPath. I pasted my code below. Is there any way I could fix this issue and stop it from crashing?

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

    let cell = tableView.dequeueReusableCellWithIdentifier("BCell", forIndexPath: indexPath!) as! BlogCell

    if let object : PFObject = self.blogPosts.objectAtIndex(indexPath!.row) as? PFObject {

        cell.author.text = object["blogger"] as? String
        cell.post.text = object["blogPost"] as? String
        let dateUpdated = object.createdAt! as NSDate
        let dateFormat = NSDateFormatter()
        dateFormat.dateFormat = "h:mm a"

        cell.timer.text =  NSString(format: "%@", dateFormat.stringFromDate(dateUpdated)) as String



        let like = object[("likes")] as! Int
        cell.likeTracker.text = "\(like)"


    }
    return cell
}

Solution

  • I would inspect what's going on with the object if I were you. You clearly aren't getting data that you expected to be there. As a stopgap, you can change let like = object["likes"] as! Int to

    if let like = object["likes"] as? Int {
       cell.likeTracker.text = "\(like)"
    }
    

    If you do that, you will also want to implement the prepareForReuse method in BlogCell to set that label's text to nil or else you might have some weird cell reuse bugs.