如何快速使类符合协议? [英] How do I make a class conform to a protocol in swift?

查看:78
本文介绍了如何快速使类符合协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了实现委托,我需要使一个类符合Swift中的协议.我该怎么办?

I need to make a class conform to a protocol in Swift in order to implement a delegate. How would I do so?

推荐答案

class YourClass: SuperClassIfAny, FirstProtocol, SecondProtocol {
}

不过请注意,某些协议要求来实现委托方法.例如,UITableViewDataSource 要求您实施

Note, though, that some protocols require you to implement delegate methods. For instance, UITableViewDataSource requires you to implement

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

如果那些不是由符合协议的类实现的,则Xcode会给您一个编译错误(始终检查协议声明,Cmd + Click将向您显示您必须实现的方法).

If those are not implemented by the class conforming to the protocol, Xcode will give you a compile error (always check the protocol declaration, Cmd + Click will show you what methods you must implement).

这篇关于如何快速使类符合协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆