2

I was trying to implement swift's alternative to the respondsToSelector: syntax that was also shown in the keynote.

I have the following:

protocol CustomItemTableViewCellDelegate {
    func changeCount(sender: UITableViewCell, change: Int)
}

and then later in the code I call

class CustomItemTableViewCell: UITableViewCell {

   var delegate: CustomItemTableViewCellDelegate
   ...
   override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
      ...
      delegate?.changeCount?(self, change: -1)
   }
   ...
}

I get the following errors

  • Operand of postfix '?' should have optional type; type is '(UITableViewCell, change:Int) -> ()'
  • Operand of postfix '?' should have optional type; type is 'CustomItemTableViewCellDelegate'
  • Partial application of protocol method is not allowed

What I am doing wrong?

Thanks

2 Answers 2

9

You have two ? operators, and they're both causing problems.

First, the one after delegate indicates that you want to unwrap an optional value, but your delegate property isn't declared that way. It should be:

var delegate: CustomItemTableViewCellDelegate?

Second, it looks like you want your changeCount protocol method to be optional. If you do, you need to both mark the protocol with the @objc attribute and mark the function with the optional attribute:

@objc protocol CustomItemTableViewCellDelegate {
    optional func changeCount(sender: UITableViewCell, change: Int)
}

(Note: Classes that conform to @objc protocols need to be @objc themselves. In this case you're subclassing an Objective-C class, so you're covered, but a new class would need to be marked with the @objc attribute.)

If you only want the delegate to be optional (that is, it's okay to not have a delegate, but all delegates need to implement changeCount), then leave your protocol as is and change that method call to:

delegate?.changeCount(self, change: -1)
3
  • Great answer. Is there any official documentation on this that you know of? I have been looking for this information for a long time!
    – Joride
    Sep 10, 2014 at 16:20
  • 1
    Apple's Swift book has a short section about optional requirements for protocols, and a chapter on optional chaining.
    – Nate Cook
    Sep 10, 2014 at 16:25
  • Thanks, just what I needed!
    – Joride
    Sep 10, 2014 at 19:11
0

The error says it all.

You use ? on an explicit type, it can't be nil, so simply don't use ? on that variable.

If you have a var like this one

var changeCount: Int

or this

var changeCount = 3

You have an explicit type. When an explicit type is requested, then you should give an explicit type, which is changeCount and not changeCount?.

If you want optional variable to begin with, declare it with an ?:

var changeCount: Int?

You can't use literal syntax with optional type if the type should be implicit. Because 3 is always explicit Int if not stated otherwise.

2
  • How can I make the delegate optional?
    – Joseph
    Jun 10, 2014 at 14:04
  • @Casper var delegate: CustomItemTableViewCellDelegate?
    – Firo
    Jun 10, 2014 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.