iOS runtime mechanism to add attributes to categories (category), replace the method of the original class execution

Source: Internet
Author: User

First, the runtime understanding

OC is an object-oriented language This is common sense, in fact, through the runtime mechanism to dynamically create classes and objects, here is simply the use of runtime usage!

Categories (category) Add attribute _ Remember to import header file before use #import <objc/runtime.h>

Dynamic binding properties through these three methods

Objc_exportvoidObjc_setassociatedobject (ID Object,Const void*key,IDvalue, Objc_associationpolicy policy) Objc_available (10.6,3.1,9.0,1.0);//set method
Objc_export
IDObjc_getassociatedobject (ID Object,Const void*key) Objc_available (10.6,3.1,9.0,1.0);//get method
Objc_export
voidObjc_removeassociatedobjects (ID Object) objc_available (10.6,3.1,9.0,1.0);//Removal

eg

- (void) SetName: (NSString *) name{//objc_association_assign = 0,//Assign//objc_association_retain_nonatomic = 1,//retaion, Nonatomic//objc_association_copy_nonatomic = 3,//Copy, Nonatomic//Objc_association_retain = 01401,//retain//objc_association_copy = 01403//Copy   /** Implement the Set method @param object#> self description#> The key value of the Key#> property is uniform on the line description#> @param value The value of the #> property description#> @param policy#> and copy,retain,assign similar to description#>*/Objc_setassociatedobject (self, Namekey, name, objc_association_copy_nonatomic);}//defining the key values of a constant property uniformStatic Char*namekey ="Namekey";-(NSString *) name{/** Implement the Get method @param object#> This is the description#> that fills in the key value of the Key#> property of the @param description#>*/    returnObjc_getassociatedobject (self, namekey);

Three, two methods of execution replacement

    SEL originalselector = nsselectorfromstring (@ " Original method name ");     = nsselectorfromstring (@ " method name to replace ");     class ], originalselector);     class ], swizzledselector);    method_exchangeimplementations (Originalmethod, Swizzledmethod);

iOS runtime mechanism to add attributes to categories (category), replace the method of the original class execution

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.