ios - 是否有可能在NSMutableArray中混淆addObject :?

标签 ios objective-c nsmutablearray

是否可以混淆NSMutableArray的addObject:方法?

这是我正在尝试的。

#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];  
}

我能够处理诸如setObject:atIndexedSubscript:之类的某些方法,但是我担心我无法做到addObject:和其他方法。
我认为下面不能被吞噬吗?有人可以解释为什么吗?我做错了什么或解决方法?
/****************   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

最佳答案

您可以使用NSProxy尝试此操作,但是我不建议您在生产代码上使用它,因为:

  • 它将破坏某些内容(某些框架可能要求NSMutableArray在向其中添加nil时抛出异常,以防止以后出现更严重的错误。即Fail fast)
  • 很慢

  • 如果您真的想避免nil检查,建议您创建NSMutableArray的子类,并在代码中的任何地方使用它。但是真的吗使用NSMutableArray的ObjC代码太多,其中大多数不需要此功能。那你为什么这么特别?
    #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
    

    关于ios - 是否有可能在NSMutableArray中混淆addObject :?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710197/

    相关文章:

    ios - 如何设置重复本地通知的开始和结束时间?

    javascript - 使用 JavaScript 核心框架从 JavaScript 调用 native 目标方法

    uitableview - 将 NSMutablearray 值加载到 TableView 中的问题?

    ios - UITextField,在ios中的文本后设置占位符

    ios - 仅当捏住单元格的 imageView 时,才可以在 UICollectionView 的自定义单元格中放大/缩小 UIImageView?

    objective-c - [NSMutableArray objectAtIndex :]: index 0 beyond bounds for empty array'

    IOS 根据 NSMutableArray 值更改 Uitableview 中的 Accessory View Cell Image

    objective-c - 在 ViewController 之间传递 NSArray。是通过指针还是通过复制?

    ios - 在具有自定义 TableViewCell 的单个 ViewController 中使用多个 UITableview

    iphone - 将值传递给目标 Controller