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