我如何让一个类快速符合委托?
我需要让一个类符合Swift中的委托。 我会怎么做?
class YourClass: SuperClassIfAny, FirstProtocol, SecondProtocol { }
但请注意,有些协议要求您实施委托方法。 例如, UITableViewDataSource
要求你实现
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
和
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
如果那些不符合协议的类实现,Xcode会给你一个编译错误(总是检查协议声明,Cmd + Click将显示你必须实现的方法)。
Xcode 6 beta 7稍微改变了协议UITableViewDataSource匹配在两个所需的实现以下语法:
6b7: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
比起6b6
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell
关键区别在于,UITableView,NSIndexPath和UITableViewCell不再是“ 隐式解包的可选项 ”