0

I have a class in swift with the following signature:

class CommentsVC: UITableViewController {

Giving me:

Class 'CommentsVC' has no initializers

It has the two bellow IBOutlets as well as two IBActions:

@IBOutlet weak var AddCommentView: UIView!
@IBOutlet weak var AddingCommentTextField: UITextField!

I also have two table view methods:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

And

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

I have looked here however it does not work for this situation as I already do what they recommend.

What is going wrong and how can I fix it?

Update:

The below code seems to be the problem. On the other VC, which uses a prepare method to pass over this data, I am passing in a post object which is initialized the same way I am initializing the problematic code below. How do I fix this?

var selectedMedia : Media {
    didSet {
        self.comments = selectedMedia.comments
        loadComments()
    }
}
Bilaal Rashid
  • 828
  • 2
  • 13
  • 21

2 Answers2

0

Just add an initial value for var selectedMedia, something like this:

var selectedMedia: Media = Media() {
    didSet {
        self.comments = selectedMedia.comments
        loadComments()
    }
}

or you can make selectedMedia optional: Media! or Media?

vsnv
  • 111
  • 3
0

This problem is pretty simple to solve. All you have to do is add a force unwrap or an optional like so:

var selectedMedia : Media! /*or, ?*/ {
    didSet {
        self.comments = selectedMedia.comments
        loadComments()
    }
}

You'll get some errors, these will be easy just press fix from suggested options to fix them (basically they will say add an "!" etc..)

  • Also remember to add the "!" infront of selectedMedia[here].comments... –  Mar 09 '19 at 03:54