///////////////////////////////////////////////////////////////////////////////////////////////////
#import "Nsarray+swizzle.h"
@implementation Nsarray (Swizzle)
-(ID) mylastobject
{
ID ret = [ self mylastobject];
NSLog(@ "-----mylastobject-------");
return ret;
}
@end
////////////////////////////////////////////////////////////
#import "Nsarray+swizzle.h"
#import <objc/runtime.h>
Method ori = class_getinstancemethod([nsarray class], @selector( Lastobject));
Method new = Class_getinstancemethod([nsarray class], @selector( Mylastobject));
method_exchangeimplementations(ori, new);
nsarray * array = @[@ "0",@ "1",@ "2",@ "3"];
nsstring * str = [array lastobject];
NSLog (@ "str:%@", str);
Output
-----mylastobject-------
Str:3
Explanation: The above code will be lastobject with Mylastobject.
So [array Lastobject] This sentence call should be mylastobject, and mylastobject inside call Mylastobject is actually called Lastobject.