我是Swift的初学者,并且真的遇到了这个问题。
我有一个原型单元CurrencySwitchTableViewCell
,它是UITableViewCell
的子类。
class CurrencySwitchTableViewCell: UITableViewCell {
@IBOutlet weak internal var currencySwitchDelegate: AnyObject!
此单元格的currencySwitchDelegate
属性应为CurrencySwitchDelegate
协议
protocol CurrencySwitchDelegate {
func didSelectCurrency(currency:Currency)
}
如何在CurrencySwitchTableViewCell
中声明我的currencySwitchDelegate
AnyObject
与CurrencySwitchDelegate
协议相对应?
什么是Swift类似的Objective-C代码?
NSObject<CurrencySwitchDelegate>
或id<CurrencySwitchDelegate>
P.S。
我知道我可以宣布我的财产是
@IBOutlet weak internal var currencySwitchDelegate: CurrencySwitchDelegate!
但是XCode给了我@IBOutlet
修饰符的错误(IBOutlets应该是AnyObject
类)
答案 0 :(得分:1)
对象通常对委托具有弱引用,并且只有对象可以成为弱引用。您可以使用: class
protocol CurrencySwitchDelegate: class {
func didSelectCurrency(currency:Currency)
}
目前Xcode无法通过IBOutlet协议。临时解决方案是创建一个AnyObject类型的其他IBOutlet,然后将其强制转换为代码。
@IBOutlet weak internal var outletDelegate: AnyObject?
private var delegate: CurrencySwitchDelegate? {
return self.outletDelegate as! CurrencySwitchDelegate?
}