The things that ios-runtime ... Edit in ....

Source: Internet
Author: User

Runtime-ios black Magic, still very fun, message mechanism, method replacement simple record a little, continuous update ....

1. Method substitution

In the class load method, replace the system method

+ (void) load{Method Oldcolormethod= Class_getinstancemethod ([selfclass], @selector (setbackgroundcolor:)); Method Newcolormethod= Class_getinstancemethod ([selfclass], @selector (xg_setbackgroundcolor:)); ///Exchange Method///calling the system's SetBackgroundColor method executes the Xg_setbackgroundcolor methodmethod_exchangeimplementations (Oldcolormethod, Newcolormethod);}- (void) Xg_setbackgroundcolor: (Uicolor *) color{///do not call the system's SetBackgroundColor method in this method to prevent circular referencing[self xg_setbackgroundcolor:color];}

2. Dynamically generated properties
-(void) Setnamexg: (NSString *) namexg{    "namexg", Namexg, objc_ association_retain_nonatomic);} -(NSString *) namexg{    return"namexg");}
3. The realization of the dictionary turn model
#import "Nsobject+xgdictionary.h"#import<objc/runtime.h>@implementationNSObject (xgdictionary)+ (Instancetype) xg_modelkeyvalues: (Nsdictionary *) dicdata{IDModel =[[Self alloc]init]; unsignedintCount =0; ///returns all properties and variables for a classIvar *ivarsa = Class_copyivarlist (self, &count);  for(inti =0; I < count; i + +) {Ivar IV=Ivarsa[i]; NSString*ivarname =[NSString Stringwithutf8string:ivar_getname (iv)]; Ivarname= [Ivarname Substringfromindex:1]; IDValue =Dicdata[ivarname];    [Model Setvalue:value Forkeypath:ivarname]; }    returnmodel;}@end

The things that ios-runtime ... Edit in ....

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.