In the swift language, the difference and connection between any,anyobject,anyclass

Source: Internet
Author: User

In the swift language, a protocol defines a set of variables and methods that a class or struct should adhere to, as follows, a declaration of a standard protocol:

protocol Nsobjectprotocol {func isequal (Object: Anyobject?) -Bool var hash:int {Get} var superclass:anyclass? {Get} func ' self ' ()self!func isproxy ()-Bool func iskindofclass (aclass:anyclass)-Bool func ismemberofclass (aclass:anyclass)-Bool func conformstoprotocol (aprotocol:protocol)-Bool func respondstoselector (aselector:selector)-Bool var description:string {Get} optional var debugdescription:string {Get }}

In the protocol, you can declare a property, such as superclass, which is a read-only property, and you can declare methods such as IsEqual, IsProxy, and so on, and the optional keyword, which indicates that this property is not enforced. The comma outside of ' self ' is surrounded by a syntax keyword, and if the keyword wants to be used as a variable name, it must be comma.

Familiar with the agreement, OK, the point is, any, anyobject, anyclass What is the difference:

    1. 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.
    2. Anyobject is a member of an empty protocol, and any object implements this protocol.
    3. Anyclass is the nickname of Anyobject.type.

Take a look at the declaration of any, in Swift:

A protocol type that any type adheres to

Typealias any = protocol<>

Because of its empty, and become its big, Lao Tzu said, I do not have any rules, so anyone can abide by me. Protocol is a keyword that is used to place multiple protocols within the angle brackets, and the following code is syntactically:

var SSS3: Protocol < Nsobjectprotocol , LYProtocol1> = LYNSObject1()

Here's a look at the definition of Anyobject:

@objc Protocol Anyobject {

}

This protocol does not define any members. @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 uses @objc to declare attributes because it is meant to be a messenger of OBJC and Swift, and many of the types of passing variables are anyobjct.

Unlike Any,anyobject, which is an empty protocol that does not declare any member, any represents an empty set of protocols. Anyobject is a protocol, and any is a 0 protocol.

Finally look at a definition of Anyclass:

Typealias Anyclass = Anyobject. Type

It's really just an alias. Any is also an alias and an alias for protocol<>.

The following code can deepen the understanding:

Protocol lyprotocol1{//some code}classlynsobject1:nsobject,lyprotocol1{//some code}var Ly1:anyvar Ly2:protocol<>Typealias Any1= protocol<nsobjectprotocol>var k:protocol<NSObjectProtocol>var k1:nsobjectprotocolvar sss1:protocol<NSObjectProtocol> =nsobject () var sss2:nsobjectprotocol=NSObject ()//var sss1:protocol<nsobjectprotocol,lyprotocol1> = NSObject ()//of the wrongvar sss3:protocol<nsobjectprotocol,lyprotocol1> = LYNSObject1 ()//feasible

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

Learn Swift technology, join the Swift book Friends QQ Group: 259152129, learn to discuss with the author!


In the swift language, the difference and connection between any,anyobject,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.