Runtime Summary Black Magic

Source: Internet
Author: User

(a) The method of setting up the picture of the interception Exchange system

#import <UIKit/UIKit.h>@interface UIImage (myimage)+ (UIImage *) Thenewimagename: ( NSString *) name; @end
#import "uiimage+myimage.h"#import<objc/runtime.h>@implementationUIImage (myimage)+ (void) load{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{        //how to swap settings for a picture#pragmaMark---If you need to exchange the method is "-" instance method with Class_getinstancemethod, if the "+" class method is used Class_getclassmethodMethod Originmethod=Class_getclassmethod (Self, @selector (imagenamed:)); Method Custommethod=Class_getclassmethod (Self, @selector (thenewimagename:));    Method_exchangeimplementations (Originmethod, Custommethod); });}+ (UIImage *) Thenewimagename: (NSString *) name{NSString*thename = [NSString stringWithFormat:@"new_%@", name]; return[UIImage thenewimagename:thename];}@end

(ii) Adding attributes to System classes

#import <Foundation/Foundation.h>@interface  nsdictionary (mydictionary)//  Add attribute @property to dictionary class (nonatomic, copy) NSString **gender; @end
#import "Nsdictionary+mydictionary.h"#import<objc/runtime.h>@implementationnsdictionary (mydictionary)/** In the extension there are * objects: object to be associated key: The key value of the property to be associated with Polocy: is the enum type enum {objc_association_assign = 0, the Property object being decorated is a weakly referenced Objc_association_retain_nonatomic = 1, strong reference, non-thread-safe objc_association_copy_nonatomic = 3, specifying the associated replication object, non-thread-safe objc_association _retain = 01401, specifying a strong reference to the associated object, thread-safe default Atomic objc_association_copy = 01403 Specifies the related replication object, which is atomic} by default; */- (void) SetName: (NSString *) name{objc_setassociatedobject (self, @selector (name), name, objc_association_copy_nonatomic);}-(NSString *) name{returnObjc_getassociatedobject (self, @selector (name));}- (void) Setgender: (NSString *) gender{objc_setassociatedobject (self, @selector (gender), gender, objc_association_copy_nonatomic);}-(NSString *) gender{returnObjc_getassociatedobject (self, @selector (gender));}@end

(iii) Use runtime to archive class attributes

#import <Foundation/Foundation.h>@interface  person:nsobject-(void) Eat; -(void**gender; @property (nonatomic, assign) Nsinteger age; @end
//Archive- (void) Encodewithcoder: (Nscoder *) acoder{unsignedintCount =0; //1. Get an array of variable listsIvar *ivarlist = Class_copyivarlist ([selfclass], &count); //2. Cycle     for(inti =0; I < count; i++) {        //3. Get the variable nameIvar Ivar =Ivarlist[i]; //4. Converting a C-language string to an OC stringNSString *ivarstring =[NSString Stringwithutf8string:ivar_getname (Ivar)]; //5. Archive[Acoder encodeobject:[self valueforkey:ivarstring] forkey:ivarstring]; }}//Anti-archiving-(Instancetype) Initwithcoder: (Nscoder *) adecoder{ Self=[Super Init]; if(self) {unsignedintCount =0; Ivar*ivarlist = Class_copyivarlist ([selfclass], &count);  for(inti =0; I < count; i++) {Ivar Ivar=Ivarlist[i]; NSString*ivarstring =[NSString Stringwithutf8string:ivar_getname (Ivar)]; //Anti-archiving[self Setvalue:[adecoder decodeobjectforkey:ivarstring] forkey:ivarstring]; }    }    returnSelf ;}

Runtime Summary Black Magic

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.