0

I am Swift beginner and really stuck with this problem.

I have a prototype cell CurrencySwitchTableViewCell that is subclass of UITableViewCell.

class CurrencySwitchTableViewCell: UITableViewCell {
       @IBOutlet weak internal var currencySwitchDelegate: AnyObject!

This cell has a currencySwitchDelegate property that should be of CurrencySwitchDelegate protocol

protocol CurrencySwitchDelegate {
   func didSelectCurrency(currency:Currency)
}

How can I declare in CurrencySwitchTableViewCell that my currencySwitchDelegate is AnyObject corresponding to CurrencySwitchDelegate protocol?

What is Swift analog of Objective-C code like this?

NSObject<CurrencySwitchDelegate> or id<CurrencySwitchDelegate>

P.S.

I know I can just declare my property to be

@IBOutlet weak internal var currencySwitchDelegate: CurrencySwitchDelegate!

But XCode gives me error with @IBOutlet modifier (IBOutlets should be of AnyObject class)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47
  • I too am a beginner with Swift, but don't you need to declare then that the `CurrencySwitchDelegate` is a protocol of `AnyObject`? – Dan F Nov 04 '15 at 23:00
  • why do you use @IBOutlet? are you connecting something via interface builder? normally it should just be something like `var currencySwitchDelegate: CurrencySwitchDelegate?` – André Slotta Nov 04 '15 at 23:07
  • I am using iboutlet to set connection from my cell to viewcontroller that implements this protocol – Alexander Tkachenko Nov 04 '15 at 23:15
  • are you using static cells? cause if not that won't work. you have to set the delegate in code (when the cells are instantiated). that means you can and should delete that IBOutlet stuff. – André Slotta Nov 04 '15 at 23:17
  • @DanF thanks for your suggestion, it doesn't work that way (I have the same error) But I found the solution here http://stackoverflow.com/questions/24561490/swift-protocol-iboutlet-property-cannot-have-non-object-type – Alexander Tkachenko Nov 04 '15 at 23:18
  • @AndreSlotta I understand this and was doing this kind of things all my life before but wanted to try moving maximum logic to storyboard. I have dynamic cells. when uitableview deques cells from reusable deque, it sets all IBOutlet properties correctly. Why this won't work with delegate which is also defined in storyboard? – Alexander Tkachenko Nov 04 '15 at 23:20
  • I tried the solution with @objc before protocol declaration and everything works fine. But it looks strange for me and seems that setting up delegate from code is a much cleaner solution. Thanks for your help! – Alexander Tkachenko Nov 04 '15 at 23:25
  • but even with @objc before protocol declaration it is not possible to connect the outlet via storyboard, is it? – André Slotta Nov 04 '15 at 23:37
  • It's possible but having @objc before protocol forces us to use only objc-compatible types – Alexander Tkachenko Nov 04 '15 at 23:42

1 Answers1

1

An object have often a weak reference to the delegate and only objects can become weak references. You can inform the compiler that the delegate is an object by using : class

protocol CurrencySwitchDelegate: class {
   func didSelectCurrency(currency:Currency)
}

For the moment Xcode cannot IBOutlet protocol. The temporary solution is to create an other IBOutlet of type AnyObject and then cast it in your code.

@IBOutlet weak internal var outletDelegate: AnyObject?

private var delegate: CurrencySwitchDelegate? {
    return self.outletDelegate as! CurrencySwitchDelegate?
}
Jeremy Vizzini
  • 261
  • 1
  • 9