<pre name= "code" class= "OBJC" > #import <objc/runtime.h> @implementation nsobject (Extension) + (void) Swizzleclassmethod: (Class) Class Originselector: (SEL) originselector otherselector: (SEL) otherselector{Method Othermehtod = Class_getclassmethod (class, Otherselector); Method Originmehtod = Class_getclassmethod (class, Originselector); Exchange 2 methods of implementation method_exchangeimplementations (Othermehtod, originmehtod);} + (void) Swizzleinstancemethod: (Class) Class Originselector: (SEL) originselector otherselector: (SEL) otherselector{ Method Othermehtod = Class_getinstancemethod (class, Otherselector); Method Originmehtod = Class_getinstancemethod (class, Originselector); Exchange 2 methods of implementation method_exchangeimplementations (Othermehtod, originmehtod);} @end @implementation Nsarray (Extension) + (void) load{[self swizzleinstancemethod:nsclassfromstring (@ "__nsarrayi") Originselector: @selector (objectatindex:) Otherselector: @selector (hm_objectatindex:)];} -(ID) Hm_objectatindex: (Nsuinteger) INDex{if (Index < self.count) {return [self hm_objectatindex:index]; } else {return nil; }} @end @implementation Nsmutablearray (Extension) + (void) load{[self swizzleinstancemethod:nsclassfromstring (@ "__ Nsarraym ") Originselector: @selector (addobject:) Otherselector: @selector (hm_addobject:)]; [Self swizzleinstancemethod:nsclassfromstring (@ "__nsarraym") Originselector: @selector (objectatindex:) Otherselector: @selector (hm_objectatindex:)];} -(void) Hm_addobject: (ID) object{if (Object! = nil) {[Self hm_addobject:object]; }}-(ID) Hm_objectatindex: (Nsuinteger) index{if (Index < self.count) {return [self hm_objectatindex:index]; } else {return nil; }} @end
iOS Runtime Swizzle