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.
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.
Comments