Summary of Dynamic Object-related operations in object-C

Source: Internet
Author: User

Object-C (hereinafter referred to as OC) has an ID type, which is a dynamic type compared with a static type that is clearly defined.

When dynamic types and polymorphism are used (different types have methods of the same name), dynamic binding (the actual called method is determined during running) can delay many judgments to the runtime, for example, the runtime determines the object type and the type of method to call. This improves flexibility, but also brings risks. Therefore, like other object-oriented languages that support dynamic types, a mechanism must be provided for runtime judgment, this avoids running errors to some extent.

When we see a dynamic instance object, we are used to the following questions:

1. Does this object belong to a class? Or what is the object type?

2. Does this object support receiving a message? Or does the object type define a method?

And so on...

To understand the answer to these questions, let's look at some code snippets in the Protocol definition in the header file nsobject. h:

@ Protocol nsobject

-(Class) superclass;

-(Class) class;

-(ID) self;

-(ID) performselector :( SEL) aselector;

-(ID) specify mselector :( SEL) aselector withobject :( ID) object;

-(ID) specify mselector :( SEL) aselector withobject :( ID) object1 withobject :( ID) object2;

-(Bool) iskindofclass :( class) Aclass;

-(Bool) ismemberofclass :( class) Aclass;

-(Bool) respondstoselector :( SEL) aselector;

@ End

Now we can see the minus sign '-', which means these messages can be sent by the instance object. Let's go through them one by one:

-(Class) superclass;

Class is a pointer structure definition. The OC system obtains the type information from the pointer. For example, you write the following code:

Nsstring * OBJ = @ "nsstring ";

Class objclass = [OBJ class];

Id objclass1 = [OBJ class];

After debugging, you will see that the value of objclass is (class) _ nscfconstantstring, while the value of objclass1 is a pointer value, similar to (ID) 0x7fff779d73b8 (this is a result of 64-bit execution). The value of objclass is obtained by the OC system from the objclass1 pointer value, because the OC system tracks the ing between the types of each instance object.

Therefore, you can use code similar to the following to determine whether the parent types of the two object instances are consistent:

If ([obj1 supperclass] = [obj2 supperclass]) {// do something ...}

-(Class)Similarly, class is used to obtain the type information of instance objects.

-(ID) SelfTo obtain the pointer value of the instance.

 

Before talking about selector, let's take a look at the following two message definitions:

-(Bool) iskindofclass :( class) Aclass
-(Bool) ismemberofclass :( class) Aclass;

Iskindofclass, as its name implies, indicates whether the object type is of a certain type. Therefore, the two boolean values in the following code should be yes.

Nsstring * STR = "nsstring ";

Class strclass = [STR class];

Class strsuperclass = [STR superclass];

Bool isnsstring = [STR iskindofclass: strclass];

Bool isnsobject = [STR iskindofclass: strsuperclass];]

Ismemberofclass, literally, seems to be asking whether the object is a member of a certain type? Some awkwardness: (let's take a look at the code execution results to determine the actual use:

Nsstring * STR = "nsstring ";

Class strclass = [STR class];

Class strsuperclass = [STR superclass];

Bool isnsstringmember = [STR ismemberofclass: strclass];

Bool isnsobjectmember = [StrIsmemberofclass: Strsuperclass];]

The execution result is:IsnsstringmemberYes,IsnsobjectmemberIs no. Therefore, it seems that ismemberofclass is used to identify whether an object is a direct instance of a certain type, rather than an inherited instance.

Therefore, the names of the two methods should be changed as follows:

Iskindofclass->Isinstanceofclass

Ismemberofclass->Isdirectinstanceofclass

Of course, you should consider the source code when defining the interface name. For the moment, I think I have not understood the original author's intention.

 

The following describes the method definitions related to the four selectors:

-(Bool) respondstoselector :( SEL) aselector;

This method has a clear name and determines whether an object responds to the specified selector. here we can see what the selector of the SEL type is? The following definitions are available in objc. h:

Typedef struct objc_selector * sel;

Similar to class, a structure pointer is used by the OC system to save the method definition information. The OC system can query the signature information and class information of the method through this pointer. OC provides the key @ selector to obtain the SEL pointer value of a method. In the following code, isok is yes. Because nsstring is nsobject, you must be able to respond to the init method.

Nsstring * STR = @ "nsstring ";

Bool isok = [STR respondstoselector: @ selector (init)];

-(ID) performselector :( SEL) aselector;

Performselector has three variants. The first one has no parameters. Let's look at the following code:

Nsstring * STR = @ "nsstring ";

Sel message = @ selector (hash );

Id Var = [STR performselector: Message];

Id var1 = [STR hash];

The nsstring object is an nsobject object, so you can certainly execute the hash method. The values of VaR and var1 must be equal, because for the same object, the Hasse value is obtained multiple times, each time the value is the same.

The other two parameter mselector are the same, but they only adapt to the situation where the method signature has a parameter and two parameters. Assume that the add method is defined as follows:

-(ID) Add: (ID) number;

Then sel should be obtained as follows:

Sel addmessage = @ selector (add :);

Id Var = [number1 datagmselector: Add withobject: number];

 

After finishing the reception, the OC block will send messages in a way that is written and gradually get used to it ~~

 

Summary of Dynamic Object-related operations in object-C

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.