is it possible to swizzle addObject: in NSMutableArray?

假如想象 提交于 2019-12-04 18:13:00

You can try this with NSProxy, but I don't suggest you to use it on production code because:

  • it will break something (some framework may require NSMutableArray to throw exception when add nil into it to prevent more serious error later. i.e. Fail fast)
  • it is slow

If you really want to avoid nil checking, I suggest you to make a subclass of NSMutableArray and use it everywhere in your code. But really? There are so many ObjC code using NSMutableArray, most of them doesn't need this feature. So why you are so special?

#import <objc/runtime.h>

@interface XLCProxy : NSProxy

+ (id)proxyWithObject:(id)obj;

@end

@implementation XLCProxy
{
    id _obj;
}

+ (void)load
{
    Method method = class_getClassMethod([NSMutableArray class], @selector(allocWithZone:));
    IMP originalImp = method_getImplementation(method);

    IMP imp = imp_implementationWithBlock(^id(id me, NSZone * zone) {
        id obj = ((id (*)(id,SEL,NSZone *))originalImp)(me, @selector(allocWithZone:), zone);
        return [XLCProxy proxyWithObject:obj];
    });

    method_setImplementation(method, imp);
}

+ (id)proxyWithObject:(id)obj
{
    XLCProxy *proxy = [self alloc];
    proxy->_obj = obj;
    return proxy;
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
    [invocation setTarget:_obj];
    [invocation invoke];
    const char *selname = sel_getName([invocation selector]);
    if ([@(selname) hasPrefix:@"init"] && [[invocation methodSignature] methodReturnType][0] == '@') {
        const void * ret;
        [invocation getReturnValue:&ret];
        ret = CFBridgingRetain([XLCProxy proxyWithObject:CFBridgingRelease(ret)]);
        [invocation setReturnValue:&ret];
    }
}

-(NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    return [_obj methodSignatureForSelector:sel];
}

- (Class)class
{
    return [_obj class];
}

- (void)addObject:(id)obj
{
    [_obj addObject:obj ?: [NSNull null]];
}

- (BOOL)isEqual:(id)object
{
    return [_obj isEqual:object];
}

- (NSUInteger)hash {
    return [_obj hash];
}

// you can add more methods to "override" methods in `NSMutableArray` 

@end

@interface NSMutableArrayTests : XCTestCase

@end

@implementation NSMutableArrayTests

- (void)testExample
{
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:nil];
    [array addObject:@1];
    [array addObject:nil];
    XCTAssertEqualObjects(array, (@[[NSNull null], @1, [NSNull null]]));
}

@end

You can iterate over all registered classes, check if current class is a subclass of NSMutableArray, if so, swizzle.

I would advice against it, rather act on case-by-case basis to have more predictable behavior - you never know which other system frameworks rely in this particular behavior (e.g. I can see how CoreData might rely on this particular behavior)

You can swizzle any NSMutableArray method in the following way:

@implementation NSMutableArray (Swizzled)

+ (void)load
{
    Method orig = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), NSSelectorFromString(@"addObject:"));
    Method override = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(addObject_override:));

    method_exchangeImplementations(orig, override);
}

- (void)addObject_override:(id)anObject
{
   [self addObject_override:anObject];

   NSLog(@"addObject called!");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!