iOS Development written interview-the principle of runtime (i)

Source: Internet
Author: User

First of all, a brief book author's blog, the introduction of the runtime is clear enough.

http://www.jianshu.com/p/25a319aee33d

The Objective-c language uses object-oriented features and message-forwarding mechanisms.

One: Data structure of Runtime:

Parsing: Root class is actually Nsobject,nsobject is no super class, so the root class superclass point nil;

Each class has an Isa pointer pointing to the unique meta class;

The root class (meta) superclass points to the root class, i.e. NSObject, to form a loop;

The ISA pointer for each Meta class points to the root class (meta);

Note: either the sample object or class is an object of type ID (class is also an object)

The example object's Isa points to its class (all subtraction methods are stored), and the class object Isa points to the Meta class (save all the good methods)

When you send a message to an object (ID) type, you are looking for a method from the class that the ISA pointer of this object points to


Methodlists represents a list of methods, he is a level two pointer to the objc_method_list struct, can dynamically modify the value of *methodlists to add member methods, is also the category implementation principle, also resolves the reason that category cannot add a sample variable.

(Level Two pointers related reading: http://www.fenesky.com/blog/2014/07/03/pointers-to-pointers.html)

@implementation son:father-(id) init{self = [super init];        if (self) {NSLog (@ '%@ ', Nsstringfromclass ([Self class]));    NSLog (@ "%@", Nsstringfromclass ([Super class])); } return self;} @end

Two are output son, concrete look at the article analysis.

Two: about the message forwarding mechanism:

Some of the more basic uses of this can be seen directly in another example:

http://my.oschina.net/caijunrong/blog/508738

The timing of the choice of three methods the author says:

Method Resolution: Because the method Resolution can be sent to other objects like message forwarding, it is only applicable to the original class substitution;

Fast Forwarding: It can transfer message processing to other objects, using a wider range of objects, not just the original object;

Normal Forwarding: It is as fast as Forwarding can message forwarding, single it can be Nsinvocation object to get more messages sent by the message, such as: Target, selector, Information such as arguments and return values.

Three: about how to use category to extend existing categories, you can generally only add the sample method of the class method, because the two-level pointer reason, not suitable for adding additional properties. Although you can declare property properties in the category header file, the compiler in the implementation file cannot synthesize any sample variables and property access methods. A custom property access method is required, and the ASSOCIATD objects is used to add custom properties to the already existing class category.

Associated objects provides three APIs to add, get, and delete associated values to an object:

void Objc_setassociatedobject (ID object, const void *key, ID value, objc_associationpolicy policy) ID objc_getassociate Dobject (ID object, const void *key) void objc_removeassociatedobjects (ID object)

Written questions:

IOS6 level:

http://blog.sunnyxx.com/2014/03/06/ios_exam_0_key/







iOS Development written interview-the principle of runtime (i)

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.