Aashish Nagar
Aashish Nagar

Reputation: 1227

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.

Upvotes: 6

Views: 5446

Answers (2)

Artem Bobrov
Artem Bobrov

Reputation: 151

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()
}

Upvotes: 9

plivesey
plivesey

Reputation: 2367

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.

Upvotes: 14

Related Questions