Runtime Learning to Organize

Source: Internet
Author: User

There are a lot of on-line principles of the runtime mechanism has a lot of introduction, here do not repeat. This is mainly about how runtime is used, how it is used, and what scenarios we can use.

In general, we use the runtime to dynamically modify or increase the original object's properties and methods to achieve their desired results.

How does the object-c interact with the runtime? There are three ways to interact

1.objective-c Source code

For example, if we send such a [receiver message], it will actually parse into objc_msgsend (ID receiver, SEL selector) at runtime;

2. methods defined by the NSObject class (such as classes that return objects from class;Iskindofclass: and Ismemberofclass: Checks whether the object is in the specified class inheritance system;respondstoselector: Checks if the object can respond to the specified message;Conformstoprotocol: Checks whether the object implements the method of specifying the Protocol class; Methodforselector: Returns the address of the specified method implementation)

3. Direct Invocation of the runtime function

Today we highlight the direct call to the runtime function.

Since we want to use the runtime function, we will first introduce its common API, essentially runtime is a series of C language API.

1). class Object_setclass (ID obj, class CLS)/class Object_getclass (ID obj) // Change the class of object/Get object class

-(void) setclasstest

{

nserror *err = [[nserror alloc] init];

Class aclass =object_setclass(err, [nsstring class]);

NSLog(@ "aclass:%@",nsstringfromclass(AClass));

NSLog(@ "obj class:%@",nsstringfromclass([Err class]));

 }

Printing results:

Aclass:nserror

Obj class:nsstring

The class that has changed the Err object dynamically

-(void) getclasstest

{

Class alogclass =object_getclass(err);

NSLog(@ "Alogclass%@",nsstringfromclass(Alogclass));

}

Print Result:alogclass nsstring

2). Const Char *object_getclassname (ID obj) // gets the class name of the object

-(void) getclassname

{

nserror *err = [[nserror alloc] init];

nsstring *classname = [nsstring stringwithcstring:object_getclassname(Err) Encoding:nsutf8stringencoding];

NSLog(@ "classname:%@", className);

}

3). class Objc_getclass (const char *name) //Find the definition of the class by name

{

  Class Lenderclass = objc_getclass("Nserror");

}

4). Class objc_getmetaclass (const char *name) // Back to Metaclass

About this metaclass, here's a little bit about

Metaclass are classes of class objects. Each object has a metaclass, the class main storage class method, that is, "+" The beginning of the method.

When you send a message to a class ,runtime looks in the list of Meta-class methods for that class .

Further down, Meta-class is also a class, can also send a message to it, then its Isa point to what? To keep this structure from extending indefinitely, Objective-c's designers let all Meta-class Isa point to the meta-class of the base class as their owning class. That is, any meta-class under the NSObject inheritance system uses NSObject's Meta-class as its own class, while the base class's Meta-class Isa pointer is pointing to itself.

Use the online map to show it.

The red-circled part can be seen to form a perfect closed loop.

It is estimated that a lot of people are already a bit dizzy, so this article is finished first, the following code is detailed.

Runtime Learning to Organize

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.