为什么在Swift中使用仅类协议? [英] Why use class only protocols in Swift?

查看:142
本文介绍了为什么在Swift中使用仅类协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在Swift中向我解释只有类的协议。我了解协议是什么以及我们使用它们的原因。我也明白,当我们在其中使用引用类型对象时,建议使用仅类协议,并且希望仅将协议构造限制为类。但是,我找不到任何支持该建议的好答案。为什么推荐?在这种情况下使用普通协议有什么缺点。

Can anyone please explain class only protocols to me in Swift. I understand what protocols are and why we use them. I also understand that its recommended to use class only protocols when we use reference type objects in it and want to limit the protocol conformation to classes only. However, I can't find any good answer to support that recommendation. Why is it recommended? What is the drawbacks of using normal protocols in that case.

推荐答案

一个用例:


  • 您有一个委托协议,有人希望拥有该协议类型的属性。 weak 只能用于引用类型;因此,协议必须是仅限类的。

  • You have a "delegate" protocol and someone wants to have a weak property of that protocol type. weak can only be used on reference types; therefore, the protocol must be class-only.

这篇关于为什么在Swift中使用仅类协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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