是否可以在 NSMutableArray 中混合 addObject:? [英] is it possible to swizzle addObject: in NSMutableArray?
问题描述
NSMutableArray 的 addObject: 方法是否可以混合使用?
Is it possible to swizzle the addObject: method of NSMutableArray?
这就是我正在尝试的.
#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: 和其他方法.我认为下面不能调配?有人可以解释为什么吗?我做错了什么或解决这个问题的方法?
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
推荐答案
你可以用 NSProxy
试试这个,但我不建议你在生产代码中使用它,因为:
You can try this with NSProxy
, but I don't suggest you to use it on production code because:
- 它会破坏某些东西(某些框架可能需要
NSMutableArray
在添加nil
时抛出异常以防止以后出现更严重的错误.即快速失败) - 很慢
- it will break something (some framework may require
NSMutableArray
to throw exception when addnil
into it to prevent more serious error later. i.e. Fail fast) - it is slow
如果你真的想避免 nil
检查,我建议你创建一个 NSMutableArray
的子类并在你的代码中到处使用它.但真的吗?使用NSMutableArray
的ObjC代码太多了,大部分都不需要这个功能.那你为什么这么特别?
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
这篇关于是否可以在 NSMutableArray 中混合 addObject:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!