is it possible to swizzle addObject: in NSMutableArray?

情到浓时终转凉″ 提交于 2020-01-13 06:37:05

问题


Is it possible to swizzle the addObject: method of NSMutableArray?

Here is what I am trying.

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@implementation NSMutableArray (LoggingAddObject)


+ (void)load {
Method addObject = class_getInstanceMethod(self, @selector(addObject:));
Method logAddObject = class_getInstanceMethod(self, @selector(logAddObject:));
method_exchangeImplementations(addObject, logAddObject);

Method original = class_getInstanceMethod(self, @selector(setObject:atIndexedSubscript:));
Method swizzled = class_getInstanceMethod(self, @selector(swizzled_setObject:atIndexedSubscript:));
method_exchangeImplementations(original, swizzled);
}


- (void)logAddObject:(id)anObject {
[self logAddObject:anObject];
NSLog(@"Added object %@ to array %@", anObject, self);
}

-(void)swizzled_setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
{
NSLog(@"This gets called as expected!!-----");
[self swizzled_setObject:obj atIndexedSubscript:idx];  
}

I am able to swizzle some of the methods like setObject:atIndexedSubscript: but I am worried that I cant do it do the addObject: and others. I think the below can not be swizzled? Can someone explain why ? what I am doing wrong and or a way around this?

/****************   Mutable Array       ****************/

@interface NSMutableArray : NSArray

- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

@end

回答1:


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



回答2:


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)




回答3:


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!");
}


来源:https://stackoverflow.com/questions/23710197/is-it-possible-to-swizzle-addobject-in-nsmutablearray

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