Unexpectedly found nil while unwrapping optional value (Swift, Parse)

David

When I click the IBAction button I am getting the error "fatal error: unexpectedly found nil while unwrapping an Optional value" Thread 1:EXC_BAD_Instruction.

The crash occurs on the Downloader.sharedDownloader.upClicked(postObject) line.

I have researched this error, and it seems like the postObject variable is nil and causing the crash. I have read about optionals and tried using an if let statement and a != nil statement, but neither of these suggestions have solved the problem.

I do not understand why the postObject variable returns nil in my TableViewCell but not in my CommentingViewController file

Why is my postObject returning nil in my TableViewCell file and how might I fix it?

class TableViewCell: UITableViewCell {    

 var number:Int = 0

 var postObject: PFObject!

@IBOutlet weak var topButton: UIButton!


@IBOutlet weak var postText: UILabel!


@IBOutlet weak var counts: UILabel!


@IBAction func upvote(sender: UIButton) {


    number += 1


    counts?.text = "\(number)"




    Downloader.sharedDownloader.upClicked(counts.text!,post: postObject)
    print("success")
   }

Here is the file where the postObject variable of the same type is used on the line Downloader.sharedDownloader.postingAComment(wf_commentingTextView.text, post: postObject) but does not cause a crash.

class CommentingViewController: UIViewController {


var postObject:PFObject!

weak var delegate: CommentingViewControllerDelegate?


@IBOutlet weak var wf_commentingTextView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handlePostingComment:", name: postCommentNotification, object: nil)
    let item = UIBarButtonItem(title: "Post", style: .Done, target: self, action: "postComment")
    navigationItem.rightBarButtonItem = item


}
// post a comment
func postComment(){
    if wf_commentingTextView.text.characters.count > 0{
        Downloader.sharedDownloader.postingAComment(wf_commentingTextView.text, post: postObject)
    }

}

func handlePostingComment(notification: NSNotification){
    if let success = notification.object as? Bool{
        if success {
            delegate?.reloadComments()

        } else{

        }
    }
}


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

Here is the Downloader file where I declared the two functions being compared.

     func postingAComment(text:String, post: PFObject){

    let comment = PFObject(className: "Comments")
    comment.setValue(post, forKey:"post")
    comment.setValue(text, forKey:"text")
    comment.setValue(PFUser.currentUser(), forKey: "fromUser")

    comment.saveInBackgroundWithBlock { (success, error) -> Void in

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            NSNotificationCenter.defaultCenter().postNotificationName(postCommentNotification, object: success)
        })


    }

}

func upClicked(text:String,post: PFObject){
    let upVoteObj = PFObject(className: "Upvote")
    upVoteObj.setValue(post, forKey: "post")
    upVoteObj.setValue(text, forKey: "text")
    upVoteObj.setValue(PFUser.currentUser(), forKey: "fromUser")

    upVoteObj.saveInBackgroundWithBlock { (success, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            NSNotificationCenter.defaultCenter().postNotificationName(upVoteNotification, object: success)

        })
    }
     print("vote was saved")
}

Thank you for any help.

beyowulf

Changing this line:

 var postObject: PFObject!

To

var postObject = PFObject(className:"PostObject") //Or whatever you class is

Will get it to stop crashing, but you likely want to be passing in postObject, which you're either passing in nil or haven't set cell.postObject in your cellForRowAtIndexPath.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

fatal error: unexpectedly found nil while unwrapping an Optional value swift

Swift: error: Unexpectedly found nil while implicitly unwrapping an Optional value

Unexpectedly found nil while unwrapping an Optional value swift

Swift: UIImageView - Unexpectedly found nil while unwrapping an Optional value

Swift unit test - unexpectedly found nil while unwrapping an Optional value

Swift: “unexpectedly found nil while unwrapping an Optional value” in array append

Unexpectedly found nil while unwrapping an Optional value in Json parsing in swift

Swift Gyroscope: Unexpectedly found nil while unwrapping an Optional value

Swift unexpectedly found nil while unwrapping an optional value

Swift - Error: unexpectedly found nil while unwrapping an optional value

Swift addGestureRecognizer: Unexpectedly found nil while unwrapping an Optional value

Unexpectedly found nil while unwrapping an Optional value? Swift4

Swift3 : unexpectedly found nil while unwrapping an Optional value

fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

Swift unexpectedly found nil while unwrapping an Optional value

Playing music in Swift: Unexpectedly found nil while unwrapping an Optional value

Unexpectedly found nil while unwrapping an - Optional Value

Unexpectedly found nil while unwrapping optional value

Unexpectedly found nil while unwrapping an Optional value()

unexpectedly found nil while unwrapping an Optional value(optional binding)

Swift - While playing sound I get error "fatal error: unexpectedly found nil while unwrapping an Optional value"

Swift Unexpectedly found nil while unwrapping an optional value while trying to move up the layout

Unexpectedly found nil while unwrapping an optional value of type NSData

NSDictionary: error Unexpectedly found nil while unwrapping an Optional value

unexpectedly found nil while unwrapping an Optional value - Using ALAMOFIRE

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

UIPickerView: unexpectedly found nil while unwrapping an Optional value

NSJSONSerialization : unexpectedly found nil while unwrapping an Optional value

Unexpectedly found nil while unwrapping an optional value with SLPagingViewSwift and storyboards