Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBOutlet in protocol implementaion

I have the following protocol:

protocol TextViewInputField {
   var indexPath: IndexPath? { get set }
   var textView: UITextView { get set }
   var lblPlaceHolder: UILabel { get set }
   func updatePHHiddenState()
} 

a cell TMStyle2Cell implements this protocol as follows:

class TMStyle2Cell: UITableViewCell,TextViewInputField {

    @IBOutlet var lblPlaceHolder: UILabel!
    @IBOutlet var textView: UITextView!
    @IBOutlet var viewSeperator: UIView!
    var indexPath: IndexPath?

    func updatePHHiddenState() {

    }
}

Why am I getting the following error?

TMStyle2Cell does not confirm to protocol TextVeiwInputField.

like image 346
Aashish Nagar Avatar asked Jun 13 '17 10:06

Aashish Nagar


2 Answers

The types in your protocol and your implementation aren't matching. You need:

protocol TextViewInputField {
   var indexPath: IndexPath? { get set }
   var textView: UITextView! { get set }
   var lblPlaceHolder: UILabel! { get set }
   func updatePHHiddenState()
} 

If you use weak IBOutlets, you also need to include that:

protocol TextViewInputField {
   var indexPath: IndexPath? { get set }
   weak var textView: UITextView! { get set }
   weak var lblPlaceHolder: UILabel! { get set }
   func updatePHHiddenState()
} 

Finally, small point, but the set part of your protocol probably isn't necessary.

like image 172
plivesey Avatar answered Oct 06 '22 00:10

plivesey


Example of protocol. Tested in Swift 4.2.

@objc protocol ImageRepresentable {
    var imageView: UIImageView! { get set }
}

And for view.

class ViewA: UIView, ImageRepresentable {
    @IBOutlet weak var imageView: UIImageView!
}

For your case.

@objc protocol TextViewInputField {
    var indexPath: IndexPath? { get set }
    var textView: UITextView! { get set }
    var lblPlaceHolder: UILabel! { get set }
    func updatePHHiddenState()
}
like image 22
Artem Bobrov Avatar answered Oct 05 '22 23:10

Artem Bobrov