从 AnyObject 扩展的协议和仅限类的协议有什么区别? [英] What's the difference between a protocol extended from AnyObject and a class-only protocol?

查看:33
本文介绍了从 AnyObject 扩展的协议和仅限类的协议有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个声明

protocol SomeProtocol : AnyObject {
}

和这个声明

protocol SomeProtocol : class {
}

似乎使得只有类可以符合这个协议(即协议的实例是对对象的引用),并且没有其他影响.

seem to make it so that only classes can conform to this protocol (i.e. that the instances of the protocol are references to objects), and have no other effects.

它们之间有什么区别吗?一个应该优先于另一个吗?如果不是,为什么有两种方法可以做同样的事情?

Is there any difference between them? Should one be preferred over the other? If not, why is there two ways to do the same thing?

我使用的是最新发布的 Xcode 6.3.1.

I am using the latest released Xcode 6.3.1.

推荐答案

官方 Swift 开发人员 (Slava_Pestov) 在 Swift 论坛.总结如下:

This was answered by an official Swift developer (Slava_Pestov) on the Swift forums. Here is the summary:

  • 你应该使用AnyObject(protocol SomeProtocol: AnyObject).

AnyObjectclass 是等价的.没有区别.

AnyObject and class are equivalent. There is no difference.

class 最终会被弃用.

这篇关于从 AnyObject 扩展的协议和仅限类的协议有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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