What is the difference between Swift's entry into the pit--any, Anyobject and Anyclass

Source: Internet
Author: User

Any :Any is an alias for an empty protocol collection that indicates that no protocol is implemented, so it can be any type, including class instances and struct-body instances. Any is an alias.

///A protocol type that is respected by any type

Typealias any = protocol<>


Anyobject:

Anyobject is a member of an empty protocol, and any object implements this protocol.

Check it out. Anyobject definition of:

@objc protocol Anyobject {

}


Anyclass:

Anyclass is the nickname of Anyobject.type.

See a under definition of Anyclass:

Typealias Anyclass = Anyobject.type


@objc is a declarative attribute used to decorate any declaration that can be accessed or used in OBJC , such as non-nested classes, protocols, properties and methods in classes and protocols, initializers, destructors, and subscripts. If you apply the OBJC attribute to a class or protocol, it is also implicitly applied to the members of that class or protocol. For classes marked with the OBJC attribute, the compiler implicitly adds the OBJC attribute to its subclasses.


Anyobject is decorated with @objc declaration attributes because it is meant to be a messenger of OBJC and Swift , and there are many types of transitive variables anyobjct .

Unlike any ,anyobject is an empty protocol that does not declare any members, and is any that represents an empty set of protocols.


Summarize:

Anyobject is a protocol, any is 0 protocols! anyobject is used for any class instance , and any is for any variable.

Check it out. definition of Anyobject:

@objc protocol Anyobject {

}

See a under definition of Anyclass:

Typealias Anyclass = Anyobject.type

Anyclass-it's really just an alias. Any is also an alias and an alias for protocol<> .

What is the difference between Swift's entry into the pit--any, Anyobject and Anyclass

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.