Swift正确声明与协议相对应的对象并具有超类

时间:2015-11-04 22:56:49

标签: ios swift xcode-storyboard

我是Swift的初学者,并且真的遇到了这个问题。

我有一个原型单元CurrencySwitchTableViewCell,它是UITableViewCell的子类。

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

此单元格的currencySwitchDelegate属性应为CurrencySwitchDelegate协议

protocol CurrencySwitchDelegate {
   func didSelectCurrency(currency:Currency)
}

如何在CurrencySwitchTableViewCell中声明我的currencySwitchDelegate AnyObjectCurrencySwitchDelegate协议相对应?

什么是Swift类似的Objective-C代码?

NSObject<CurrencySwitchDelegate>id<CurrencySwitchDelegate>

P.S。

我知道我可以宣布我的财产是

@IBOutlet weak internal var currencySwitchDelegate: CurrencySwitchDelegate!

但是XCode给了我@IBOutlet修饰符的错误(IBOutlets应该是AnyObject类)

1 个答案:

答案 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?
}