Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

swift - What's the difference between a protocol extended from AnyObject and a class-only protocol?

Both this declaration

protocol SomeProtocol : AnyObject {
}

and this declaration

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?

I am using the latest released Xcode 6.3.1.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  • You should use AnyObject (protocol SomeProtocol: AnyObject).

  • AnyObject and class are equivalent. There is no difference.

  • class will eventually be deprecated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...