Runtime 10 ways to use

Source: Internet
Author: User
Tags call back

Source: Haojingxue_ios

Links: http://www.jianshu.com/p/3182646001d1

Read a number of run-time articles, feel very good, from a few articles to extract some of the more important, the partial actual knowledge points of the excerpt, in addition to combine the creation of individuals to form this article. Again the technology and real combat combined to make sense, the introduction of technology as far as possible with the actual combat links, some did not understand, I gave the link inside have, so I will not repeat ...

1) Replace the system method,

2) dictionary to model,

3) Archive,

4) Universal Controller jump

Four off-the-field methods are aggregated from different articles. I don't understand the trouble. Spectators go to the link I gave to see what they have to say

I use a picture to introduce the content of the article (figure just understand, I like, everyone likes)

Runtime Article collection:

Runtime article topic (13+ about Runtime) (HTTP://WWW.JIANSHU.COM/COLLECTION/DC947EAB6AF3)

The following 4 articles have similar small white can read the runtime, worth watching

Text ①oc most practical runtime summary, interview, work you look at me is enough!

② lets you quickly get started with runtime

Http://www.jianshu.com/p/e071206103a4

Text ③runtime detailed

Http://www.jianshu.com/p/46dd81402f63

Text ④ detailed runtime runtime mechanism

Http://www.jianshu.com/p/1e06bfee99d0

Text ⑤ Universal Controller jump

Http://www.cocoachina.com/ios/20150824/13104.html

First use a picture to do an introduction to the article (picture, easy to understand, convenient memories, I like, we also like)

What (Runtime is)

Runtime is basically written in C and sink, which can be seen in Apple's efforts to make dynamic systems efficient. You can go down here to Apple to maintain the open source code. Apple and GNU each maintain an open-source runtime version, and the two versions are struggling to keep up with each other. Objective-c interacts with the runtime system from three different levels, through the Objective-c source code, through a method defined by the NSObject class of the Foundation framework, through a direct call to the runtime function. In most cases you can just write your OBJC code, and the runtime system will work hard behind the scenes automatically.

runtime abbreviation run, is the system at the time of the operation of some mechanism, the most important is the message mechanism.

In the case of C, the call of a function determines which function is called at compile time, and executes directly in sequence after compilation, without ambiguity.

The function call of OC becomes a message sent. belongs to the dynamic call procedure. It is not possible at compile time to decide which function to actually call (it turns out that OC can call any function during the compile phase, even if the function is not implemented, as long as it is declared without an error.) C language will be in the compilation phase error).

The corresponding function will be called based on the name of the function only when it is actually running.

where (where runtime is used)

1. Turn some OC code into runtime code to explore the underlying, such as the implementation principle of block (above);

2. The interception system comes with a method call (Swizzle black magic), can also be said to replace the system method, such as interception imagenamed:, Viewdidload, alloc; Wen/Tenzianghong (book author) original link:/http Www.jianshu.com/p/ab966e8a82e2

Requirements: For example, iOS6 upgrade iOS7 need version adaptation, according to different systems using different styles of pictures (quasi-materialization and flattening), how to manually modify each UIImage imagenamed: Method can be implemented as the method to add version judgment statement?

Steps:

A. Build a classification for UIImage (uiimage+category)

b, implement a custom method in the classification method, write the statement to be added in the system method, such as version judgment [reference]

+ (uiimage*)xh_imagenamed:(nsstring*)name {

Doubleversion = [[uidevicecurrentdevice]. Systemversiondoublevalue];

If(version >=7.0) {

If the system version is more than 7.0, use a different set of filenames ending with a flattened picture of ' _os7 '

Name = [name stringbyappendingstring:@ "_os7"];  }

return[uiimagexh_imagenamed: Name];

}

C, the classification of the UIImage load method, to implement the exchange of methods (as long as it can be executed once the method exchange statements, load is more appropriate)

+ (void)load {

Get class methods for two classes

Method M1 = class_getclassmethod([uiimageclass],@selector( Imagenamed:));  

Method m2 = class_getclassmethod([uiimageclass],@selector(xh_ Imagenamed:));

Start Exchange Method implementation

Method_exchangeimplementations(M1, m2);

}

Note: In the end of the custom method, we must call back the system method, let it have the function of loading the picture, but because of the method exchange, the system's method name has become our custom method name (a bit around, is the method that can call the system with our name, can call our method with the system name), This enables the interception of system methods!

Using the above ideas, we can also add categories to the NSObject, statistics created how many objects, to add categories to the controller, statistics on how many controllers have been created, especially when the company needs the total change, in some of the original control or module to add a function, it is recommended to use this method!

3. The implementation of the classification can also add attributes;

4. Implementation of Nscoding automatic archiving and automatic file, (without Edcode and decode for each attribute, if the dozens of properties of encode and decode is really troublesome ah, using the runtime can walk through the properties of each object, the way the array traversal Eccode, Decode

Using the run-time archive method

No runtime archiving method: (Fortunately only 5 properties, if 20, 30 or the background suddenly added attributes, so directly write dead estimate code will not be the spirit)

5. Implement automatic conversion of dictionaries and models (the core is the ability to traverse through every property in the dictionary, which is used by the Daniel Framework in JSON parsing, including the Mjextension,yymodel,jsonmodel to convert JSON to a dictionary, Iterate through each of the properties in the dictionary for Modle conversions).

Mjextension part of the code excerpt using the run-time conversion JSON to model

Yymodel JSON to model core code excerpt

Jsonmodel JSON dictionary to model excerpt

The way to get a list of properties is a dictionary-to-model comparison of the core approach,

Objc_export Ivar *class_copyivarlist (class cls, unsigned int *outcount)

__osx_available_starting (__mac_10_5, __iphone_2_0);

Can this be the next conclusion?

Basically, the mainstream of the JSON to model all, using the runtime to dynamically obtain the property name of the method to replace the dictionary, the dictionary to model the most efficient (time-consuming shortest) is KVC, the other dictionary to the model is the KVC key and value to do processing, the dynamic acquisition of JSON In the process of conversion, of course, the third-party framework needs to do some KVC, mosaic logic processing, and then carry out the model. This code [XX setvalue:value Forkey:key]; no matter jsonmodle,yykit, Mjextension are indispensable [xx setvalue:value forkey:key]; This code, do not believe can go to search, this is the dictionary to model the core method,

6) Dynamic Add method (dynamically adds a method to a class or object, which is described in detail in the excerpt article)

7) Dynamic Variable control (dynamically replace the value of a variable for an object, the excerpt article is described in detail)

8) Achieve Universal Controller jump

Product to a perverted demand, push over the message, to jump to any controller. Using runtime to dynamically generate objects, properties, methods This feature, we can first negotiate with the server, define the jump rules, such as to jump to a controller, need to pass the property ID, type, then the server returned to me the dictionary, There is a controller name, two property names and property values, the client can generate objects according to the controller name, and then use KVC to assign values to the object, so it's done.

9) Plug-in development

Getting Started with plugins

XCode has a very pit dad place, is it does not officially support plug-in development, the official no documents, Xcode also does not open source, but because Xcode is objective-c write, OC Dynamic is too strong, resulting in such a closed case folk can still make a variety of plug-ins, Its core development approach is:

Dump out all of Xcode's header files and know what classes and interfaces are in Xcode.

Through the role of the header file method name guessing method, Swizzle these methods, inserting their own code to implement the plug-in logic.

Monitor the occurrence of various events through Nsnotificationcenter.

More detailed development Tutorials online There are many articles, interested in self-search bar.

Jspath hot update is also used when running, Jspatch is basically black technology, online repair version of the bug, have used this technology, details Baidu "Jspatch", omitted 30000 words here

Runtime 10 ways to use

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.