如何使类符合swift中的协议?
我需要使一个类符合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不再是’ Implicitly Unwrapped Optionals ‘